用于'for'环的单衬?



有人知道更好的实现吗?我试图不将列表中的每个元素分配给变量,而是将其直接放在for循环中。这里有一段代码可以更好地解释。

x, y = (['a', 'b', 'c'], ['d', 'e' ,'f'])
for it in itertools.zip_longest(x, y):
print (it)

我要做的是在第二行中使用单行代替(x, y)。由于List中的列表数量不同,

mainList = (['a', 'b', 'c'], ['d', 'e' ,'f'])
for it in itertools.zip_longest(*where I want to use the single liner*):
# The single liner should be putting passing the lists in the mainList as seperate params.
print (it)

我认为这也可以在其他循环中实现。

注意:单行应该把mainList中的传递列表作为单独的参数。

您可以使用元组解包。

mainList = (['a', 'b', 'c'], ['d', 'e' ,'f'])
for it in itertools.zip_longest(*mainlist):
print (it)

最新更新