例如,我有两个列表
a = [1,2,3]
b = [4,5,6]
我想制作一个格式为
的列表cc = [[1,4],[2,5],[3,6]]
由于我是Python的新手,任何人都可以帮助我。 谢谢。
答案 0 :(得分:4)
使用zip
(示例代码为Python 2样式):
a = [1, 2, 3]
b = [4, 5, 6]
print zip(a, b)
# [(1, 4), (2, 5), (3, 6)]
Python 3风格:
a = [1, 2, 3]
b = [4, 5, 6]
print(list(zip(a, b)))
# [(1, 4), (2, 5), (3, 6)]
答案 1 :(得分:1)
如果你真的想要内部列表而不是元组,你可以使用:
a = [1,2,3]
b = [4,5,6]
c = [list(result) for result in zip(a,b)]
# c = [[1,4],[2,5],[3,6]]
答案 2 :(得分:0)
a = [1,2,3]
b = [4,5,6]
c = zip(a,b)
c = [list(k) for k in c]