在Python中将列表动态分解为变量
我在运行时创建了二维列表(任一维中的条目数未知)。例如:在Python中将列表动态分解为变量,python,reflection,list,itertools,Python,Reflection,List,Itertools,我在运行时创建了二维列表(任一维中的条目数未知)。例如: long_list = [ [2, 3, 6], [3, 7, 9] ] 我想通过从long_列表中的每个列表中获取第I个条目来遍历它: for entry in long_list.iter(): #entry will be [2, 3] then [3, 7] then [6, 9] 我知道Python的itertools.izip_longest()方法可以做到这一点。除了它为每个列表接受不同的变量之外 itertoo
long_list = [ [2, 3, 6], [3, 7, 9] ]
我想通过从long_列表中的每个列表中获取第I个条目来遍历它:
for entry in long_list.iter():
#entry will be [2, 3] then [3, 7] then [6, 9]
我知道Python的itertools.izip_longest()方法可以做到这一点。除了它为每个列表接受不同的变量之外
itertools.izip_longest(var1, var2, var3 ...)
那么,如何将长列表拆分为每个列表的不同变量,然后在运行时使用所有这些变量调用izip_longest()
>>> long_list = [ [2, 3, 6], [3, 7, 9] ]
>>> import itertools
>>> for i in itertools.izip_longest(*long_list): # called zip_longest in py3k
print(i)
(2, 3)
(3, 7)
(6, 9)
基本上,您需要在这里使用解包功能。它同样适用于zip
基本上,您需要在这里使用解包功能。它对
zip
也同样有效。您可能应该解释*正在解压列表。您可能应该解释*正在解压列表。