[英]I can't find documentation on how to iterate across items in two lists serially, just in parallel
I want to execute the same effect as the below code, but there has to be a way to do it in one (possibly nested) for loop, right?我想执行与下面代码相同的效果,但必须有一种方法可以在一个(可能嵌套的)for 循环中完成,对吗?
list_1 and list_2 have a bunch of numbers. list_1 和 list_2 有一堆数字。 I need to sort the contents into a list for odds and a list for evens.
我需要将内容分类为赔率列表和偶数列表。 The below works, but seems inelegant.
下面的工作,但似乎不够优雅。 Is there a way to combine the two for loops into one?
有没有办法将两个 for 循环合二为一?
# parse list_1
for i in list_1:
if i % 2 == 0:
list_even.append(i)
else:
list_odd.append(i)
# parse list_2
for i in list_2:
if i % 2 == 0:
list_even.append(i)
else:
list_odd.append(i)
Hello please try that it much faster and easier no need to write same operation for each list您好,请尝试它更快更容易,无需为每个列表编写相同的操作
def anyoperation(i):
if i % 2 == 0:
list_even.append(i)
else:
list_odd.append(i)
list(map(anyoperation,list_1))
list(map(anyoperation,list_2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.