使用python中的条件减去两个列表

时间:2017-08-23 19:51:13

标签: python python-3.x

我有两个列表,例如i = 7个句点:

list1 = [0,0,16.5,0,29.7,62.7,90]
list2 = [0,0,0,0,10,12,2.5]
z = [0,0,1,0,1,1,1]
p[j] = [28,10,12,5,30,15,40]
rhs = 27
y = []
if z[j] == 0:
  y.append(0)
else
    if p[j] < rhs:
       y.append(0)
    else 
       y.append([a-b for a,b in zip(list1, list2)])

print(y) 

最终结果是y = [0,0,0,0,0,19,7,00],但是我的代码没有给出。能帮我解决一下吗?

1 个答案:

答案 0 :(得分:1)

zip()所有列表在一起:

>>> [a-b if c and d >= rhs else 0 for a, b, c, d in zip(list1, list2, z, p)]
[0, 0, 0, 0, 19.7, 0, 87.5]