数据在多个列表中时的列表推导

时间:2016-04-07 16:56:02

标签: python list indexing list-comprehension

可以使用列表推导而不是以下循环吗?

a = [1,3,4,7,8,10]
b = [1,1,0,0,0,1]
c=[]

for ii in range(len(b)):
    if b[ii] == 1:
        c.append(a[ii])

1 个答案:

答案 0 :(得分:0)

a = [1,3,4,7,8,10]
b = [1,1,0,0,0,1]

print([a[ii] for ii in range(len(b)) if b[ii] == 1])