~
可以将Boolean类型值取反,这在使用boolean类型数组选择数组中固定元素时十分有用。import numpy as np a=np.array([0,0,1,1]).astype("bool") b=np.arange(4) print("b\n",b) # b # [0 1 2 3] c=b[a] print("c\n",c) # c # [2 3] print("~a\n",~a) # ~a # [ True True False False] c_=b[~a] print("c_\n",c_) # c_ # [0 1]
loser_winner = np.array([1, 0, 0, 1, 0, 1]) if [0, 0, 1] == [False, False, True]: print("1") else: print("0") # 1 print(~loser_winner.astype(np.bool)) # [ True False False False True False] mutation_idx = [True, True, True, False, False, False] loser_winner[mutation_idx] = ~loser_winner[mutation_idx].astype(np.bool) print("loser_winner", loser_winner, "loser_winner[mutation_idx]", loser_winner[mutation_idx]) # loser_winner [0 1 1 1 0 1] loser_winner[mutation_idx] [0 1 1]
本文分享自微信公众号 - DrawSky(wustcsken),作者:KenXu
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2020-06-16
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句