tmplist = [True, True, False, True, False]
我想得到[0,1,3]的结果
tmplist = [True, True, False, True, False] idx = [] for i in range(len(tmplist)): if tmplist[i] == True: idx.append(i) print(idx)
tmplist = [True, True, False, True, False] print([i for i in range(len(tmplist)) if tmplist[i] == True])
pythonic solution should be as followint: from itertools import compress mask = range(5) tmplist = [True, True, False, True, False] print(list(compress(mask, tmplist))
from itertools import compress tmplist = [True, True, False, True, False] mask = [] for i in range(5): mask.append([i for i in range(len(tmplist)) if tmplist[i] == True]) print(list(compress(mask, tmplist)))
要的是下面的结果?
[[0, 1, 3], [0, 1, 3], [0, 1, 3]]
嗯,非常感谢