【发布时间】:2020-07-02 13:53:31
【问题描述】:
我想将列表列表 a 中的项目与列表 x 中的项目进行比较。这个想法是根据一个条件逐项进行比较,然后根据条件是否满足执行一些操作。但是,我必须遍历这些项目的想法不起作用。有没有有效的方法来做到这一点?
x = [10, 11, 12]
a = [[11, 10, 12], [12, 15, 20], [11, 14, 16]]
for i, j in a, b: # I am looking for an alternative way to do this
counter = []
if i <= j: # if the item in a is equal to or smaller than the corresponding
# item in list x, then the list is rejected and the counter is
# increased by 1
counter =+ 1
else:
print(counter, np.mean(a[-1])) # print the number of rejected lists
我期待的结果是:
1a的列表1中,第2项小于x中的第2项和第3项。
18另外两个列表不满足条件所以没问题,它们最后一项的平均值是(20 + 16)/ 2 = 18
【问题讨论】:
-
请让我知道为什么这个问题被否决了,这样我下次可以发布更好的问题。
标签: python-3.x numpy for-loop