[英]Compare 2 lists, based on index from 2 other lists, and save index in new list with list comprehension
So what I am trying to do is:所以我想做的是:
1) Find where list1[y] == list2[x] 1) 找到 list1[y] == list2[x]
2) Determine if list3[y] < (0.4 list4[x]) 2) 确定是否 list3[y] < (0.4 list4[x])
3) If so, store the index y in a new list of indexes 3) 如果是,则将索引 y 存储在新的索引列表中
This loop works, however it takes almost a full minute to run with my data set.这个循环有效,但是用我的数据集运行几乎需要整整一分钟。 I want to know if I can do this with list comprehension.
我想知道我是否可以通过列表理解来做到这一点。
Thanks in advance for anybody's help.预先感谢任何人的帮助。
for y in range(len(list1)):
for x in range(len(list2)):
if list1[y] == list2[x]:
if list3[y] < (0.4 * list4[x]):
list5.append(y)
我不确定这是否会显着提高性能,但试试这个:
list5 = [y for y, val1 in enumerate(list1) for x, val2 in enumerate(list2) if val1 == val2 and list3[y] < (0.4 * list4[x])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.