将列表列表与单个列表Python

时间:2015-09-29 09:28:49

标签: python list

我仍然在学习Python及其术语,所以也许我问我的问题是错误的。我有一些代码可以产生如下结果:

['S 5.3', 0]   
['S 5.4', 10]    
['S 5.5', 20]    

有了这个,我假设这是一个列表列表。我如何将它们组合成一个单独的列表,如:

[['S 5.3', 0], ['S 5.4', 10], ['S 5.5', 20]]

3 个答案:

答案 0 :(得分:3)

你应该看一下documentation。这只是一个示例(我将让您通过文档了解如何使用,例如,在此方案中使用append方法):

代码:

list1 = ['S 5.3', 0]   
list2 = ['S 5.4', 10]    
list3 = ['S 5.5', 20]  

nested_lists = [list1, list2, list3]
print(nested_lists)

输出:

[['S 5.3', 0],['S 5.4', 10],['S 5.5', 20]]

答案 1 :(得分:0)

您是否研究过appendextend方法

以下example显示了append()方法的用法。

aList = [123, 'xyz', 'zara', 'abc'];
aList.append( 2009 );
print "Updated List : ", aList
When we run above program, it produces following result −

Updated List :  [123, 'xyz', 'zara', 'abc', 2009]

以下example显示了extend()方法的用法。

aList = [123, 'xyz', 'zara', 'abc', 123];
bList = [2009, 'manni'];
aList.extend(bList)

print "Extended List : ", aList 
When we run above program, it produces following result −

Extended List :  [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']

答案 2 :(得分:0)

look into itertools module 

chain

from itertools import  chain

list1 = ['S 5.3', 0]
list2 = ['S 5.4', 10]
list3 = ['S 5.5', 20]

result = chain(list1, list2, list3)

print(list(result))

['S 5.3', 0, 'S 5.4', 10, 'S 5.5', 20]