Python-组合两个列表来制作列表列表

时间:2017-04-09 02:54:38

标签: python

我想将两个列表组合成一个列表列表。反之亦然。我找不到任何似乎有用的东西,而且我是Python的新手

示例:

S1 = [1,2,3]
S2 = [4,5,6,7]

Expected output 
S = [[1,2,3],[4,5,6,7]]

如何将S拆分回原始S1和S2? 例如:

S = [[1,2,3],[4,5,6,7]]
Expected output
S1 = [1,2,3]
S2 = [4,5,6,7]

1 个答案:

答案 0 :(得分:3)

这是最简单的解决方案。

>>> S1 = [1,2,3]
>>> S2 = [4,5,6,7]
>>> S = [S1, S2]
>>> S
[[1, 2, 3], [4, 5, 6, 7]]

重新获取列表:

>>> S1 = S[0]
>>> S2 = S[1]
>>> S1
[1, 2, 3]
>>> S2
[4, 5, 6, 7]