回答
我最喜歡aix的解決方案。這裏是另一種方式,我認爲在2.2應該工作:
>>> x=range(3)
>>> x
[0, 1, 2]
>>> y=range(7,10)
>>> y
[7, 8, 9]
>>> sum(zip(x,y),[])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
>>> sum(map(list,zip(x,y)),[])
[0, 7, 1, 8, 2, 9]
和另一種方式:
>>> a=[x,y]
>>> [a[i][j] for j in range(3) for i in (0,1)]
[0, 7, 1, 8, 2, 9]
和:
>>> sum((list(i) for i in zip(x,y)),[])
[0, 7, 1, 8, 2, 9]
備選:
>>> l1=[1,2,3]
>>> l2=[10,20,30]
>>> [y for x in map(None,l1,l2) for y in x if y is not None]
[1, 10, 2, 20, 3, 30]
該作品因爲map並行工作在列表上。它在2.2下面works the same。就其本身而言,與None
作爲被叫功能,map
產生元組的列表:
>>> map(None,l1,l2,'abcd')
[(1, 10, 'a'), (2, 20, 'b'), (3, 30, 'c'), (None, None, 'd')]
然後,只需壓平元組的列表。
的優點,當然,是map
將用於任何數目的列表的工作,並且將工作,即使它們具有不同的長度:
>>> l1=[1,2,3]
>>> l2=[10,20,30]
>>> l3=[101,102,103,104]
>>> [y for x in map(None,l1,l2,l3) for y in x if y in not None]
[1, 10, 101, 2, 20, 102, 3, 30, 103, 104]
'也會過濾掉'0',如果'y不是'則不太脆弱。 –
@Jochen Ritzel:謝謝!我贊同你。固定。我只寫了一些肌肉...... –
用於Python> = 2.3,有extended slice syntax:
>>> a = [0, 2, 4, 6, 8]
>>> b = [1, 3, 5, 7, 9]
>>> c = a + b
>>> c[::2] = a
>>> c[1::2] = b
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
行c = a + b
被用作一個簡單的方法來創建一個正確長度的新列表(在這個階段,其內容並不重要)。接下來的兩行做交織a
和b
的實際工作:第一個將a
的元素分配給所有的偶數索引c
;第二個將b
的元素分配給所有奇數索引c
。
我需要一種方法來做到這一點與接受的答案沒有解決不同大小的列表。
我的解決方案使用一臺發電機及其使用看起來有點更好,因爲它:
def interleave(l1, l2):
iter1 = iter(l1)
iter2 = iter(l2)
while True:
try:
if iter1 != None:
yield next(iter1)
except StopIteration:
iter1 = None
try:
if iter2 != None:
yield next(iter2)
except StopIteration:
iter2 = None
if iter1 == None and iter2 == None:
raise StopIteration()
及其用法:
>>> a = [1, 2, 3, 4, 5]
>>> b = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> list(interleave(a, b))
[1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e', 'f', 'g']
>>> list(interleave(b, a))
['a', 1, 'b', 2, 'c', 3, 'd', 4, 'e', 5, 'f', 'g']
有幾種選擇。
# Given
a = [1, 2, 3]
b = [10, 20, 30]
您可以itertools
得到一個交錯的列表:
import itertools
list(itertools.chain(*zip(a, b)))
# [1, 10, 2, 20, 3, 30]
考慮安裝more_itertools
- 庫附帶的interleave
,interleave_longest
和roundrobin
itertools recipe實現。
import more_itertools
list(more_itertools.interleave(a, b))
# [1, 10, 2, 20, 3, 30]
list(more_itertools.roundrobin(a, b))
# [1, 10, 2, 20, 3, 30]
最後,對於一些有趣的事情,在Python 3:
list(filter(None, ((yield from i) for i in zip(a, b))))
# [1, 10, 2, 20, 3, 30]
[el for el in itertools.chain(*itertools.izip_longest([1,2,3], [4,5])) if el is not None]
只要你沒有None
要保持
- 1. 兩個列表在Python中交互
- 2. 交換兩個Python列表
- 3. Python中兩個列表的交匯點
- 4. 如何交錯python中的兩個列表?
- 5. 在python列表中用兩個數字交換一個數字
- 6. 交換兩個子列表列表中
- 7. 在Python中相交兩個單詞列表
- 8. Python中,兩個列表
- 9. Python的 - 在兩個列表
- 10. 我如何在makefile中交錯兩個列表?
- 11. 如何在python中優雅地交錯兩列不均勻長度的列表?
- 12. F#相交兩個列表
- 13. 交換兩個列表
- 14. 在Python中轉義兩個列表
- 15. 在Python中加入兩個列表:
- 16. 在Python中比較兩個列表
- 17. 在Python中合併兩個列表
- 18. 在Python中比較兩個列表
- 19. 在兩個不同的列表中交換兩個對象
- 20. Python:交換列表中的兩個字符串
- 21. 從兩個sql server表中交換列
- 22. 兩個列表Python語言
- 23. 比較兩個列表 - Python
- 24. Python:乘兩個列表
- 25. Python的 - 從兩個列表
- 26. 返回兩個列表python?
- 27. 在Python中相交兩個詞典
- 28. Python程序將一個列表分成兩個列表,交替顯示元素
- 29. 轉換兩個列表中的詞典列表在Python
- 30. 在Python中有效比較列表的兩個列表
你知道那2.2現在十歲了?沒有理由繼續使用它。 –
@DanielRoseman:我知道。在這種情況下,我別無選擇。 – NPE
不推薦,但試試這個:'it = iter(l1);列表((yield of next(it))or i for in in in2)' –