我有兩個列表這樣創建嵌套列表:從兩個列表
t = [1,2,3,4]
f = ['apples', 'oranges','grapes','pears']
我需要創建一個列表的列表如下:
data = [
['Fruit', 'Total'],
['apples', 1],
['oranges', 2],
['grapes', 3],
['pears' 4]
]
我已經做到了這一點:
l = []
l.append(['Fruit', 'Total'])
# I guess I should have check that lists are the same size?
for i, fruit in enumerate(f):
l.append([fruit, t[i]])
只是想知道是否有更多的Pythonic這樣做。
'打印拉鍊(F,T)'你可能感興趣的,雖然它返回一個元組列表 - 未列出的清單。 – alfasin