Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Python 3 中,假设我们有 2 个列表:
list1=[a,b,c] list2=[d,e,f]
我想加入他们以获得:
new_list=[a,d,b,e,c,f]
有人能帮我吗?
>>> sum(zip(list1, list2), ()) ('a', 'd', 'b', 'e', 'c', 'f')
Ignacio 的解决方案很好,但如果您正在处理大列表:
>>> tuple(x for y in zip(list1,list2) for x in y) ('a', 'd', 'b', 'e', 'c', 'f')