[英]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.