我知道这个问题陈述了两个列表,并且没有提到numpy。但是如果你考虑使用它,并且考虑到 a 和 b 是 numpy 数组,映射操作就变得微不足道了:
a[b]
我冒昧地使用 1000x 元素对建议的选项进行基准测试:
import numpy
a = [1,2,3,4] * 1000
b = [True, True, False, True] * 1000
def question_fn():
l2_true = []
for el in range(len(a)):
if b[el] == True:
l2_true.append(a[el])
return l2_true
def suggestion_1():
return [v for i, v in enumerate(a) if b[i]]
def suggestion_2():
return [x for x,y in zip(a,b) if y]
x = numpy.array(a)
y = numpy.array(b)
def using_numpy():
return x[y]
python -m timeit -s 'import so' 'so.question_fn()'
1000 loops, best of 3: 453 usec per loop
python -m timeit -s 'import so' 'so.suggestion_1()'
10000 loops, best of 3: 203 usec per loop
python -m timeit -s 'import so' 'so.suggestion_2()'
1000 loops, best of 3: 238 usec per loop
python -m timeit -s 'import so' 'so.using_numpy()'
10000 loops, best of 3: 23 usec per loop
请注意,numpy时间不包括转换为数组,否则它会比所有其他建议的解决方案慢得多。但是,如果从一开始就使用 numpy 数组是一种选择,它可能是一个可行的解决方案。