任何人都知道對此有更好的實作。我試圖不將串列中的每個元素分配給變數,而是直接將其放入 for 回圈中。這里有一段代碼可以更好地解釋。
x, y = (['a', 'b', 'c'], ['d', 'e' ,'f'])
for it in itertools.zip_longest(x, y):
print (it)
我想要做的是使用單個襯里代替(x, y)第二行。由于串列中的串列數量各不相同。
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 中的串列作為單獨的引數傳遞。
uj5u.com熱心網友回復:
您可以使用元組解包。
mainList = (['a', 'b', 'c'], ['d', 'e' ,'f'])
for it in itertools.zip_longest(*mainlist):
print (it)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/336428.html
下一篇:嵌套for回圈以構建“0”金字塔
