[英]python: how to append multiple lists to one
我有一個列表,其中應包含所有其他列表。 目前,我將每個列表分別附加,但是看起來很丑。
looplist = [] # initiate an empty list that contains all date from the other lists
[...]
looplist.append(internallist1)
[...]
looplist.append(internallist10)
內部列表全部初始化並填充到for循環中
listOne.extend(anotherList)
這可以幫助您: https : //docs.python.org/3/tutorial/datastructures.html
您還可以執行listOne+=anotherList
,這樣比較便宜,因為它不涉及extend
類的函數調用
要回答您的問題,只需使用10個列表初始化循環列表即可。
looplist = [internallist1,
internallist2,
internallist3] #Note: internallist3,] is also valid, python allows trailing comma. Nifty!
但是,如果這是您的實際用例,那么實際上不應首先將10個列表單獨命名為列表。 只需使用looplist[0] through looplist[9]
即可。
zip
方法可以為您工作,因為您說您的輸出應該:
看起來像[[list1],[list2],...,[list n]]
在您的情況下,代碼將類似於
looplist = list(zip(internallist1,[...], internallist10))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.