繁体   English   中英

单个语句中的For循环

[英]For loops in single statement

考虑下面的例子:

set_x = [1,1,1,1,1]
set_y = [2,2,2,2,2]
x += (item_x * item_y for item_x, item_y in set_x and set_y)

显示错误

TypeError: unsupported operand type(s) for +=: 'int' and 'generator'

请帮助解决此问题

您要使用zip聚合来自多个迭代器的元素:

[item_x * item_y for item_x, item_y in zip(set_x,set_y)]
# [2, 2, 2, 2, 2]

因此使用:

x += (item_x * item_y for item_x, item_y in zip(set_x,set_y))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM