[英]python iterate over variables
Is there a python built-in for iterating over some variables? 是否有内置的python用于迭代某些变量? Something like: 就像是:
def iterate_args(*args):
for arg in args:
yield arg
It would be useful for instance in the following case: 例如,在以下情况下将很有用:
x, y, z = ...
for var in iterate_args(x,y,z):
do_something(var)
Instead of allocating a list, which looks wasteful (is it?): 而不是分配一个看起来很浪费的列表(是吗?):
x, y, z = ...
for var in [x,y,z]:
do_something(var)
Haven't been able to find anything among python built-ins . 尚未在python 内置组件中找到任何东西。
Any such built-in would be much more expensive than creating a tuple, and would in fact involve creating a tuple for args
. 任何这样的内置方法都比创建元组昂贵得多,并且实际上将涉及为args
创建元组。 Just make a tuple. 只是做一个元组。
In the specific case where x
, y
, and z
are initialized like your example: 在x
, y
和z
初始化的特定情况下,例如您的示例:
x, y, z = ...
you can just iterate over whatever you unpacked instead of unpacking it. 您可以仅对解包的内容进行迭代,而无需解压缩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.