[英]How to zip two differently sized lists, repeating the shorter list?
我想要 zip 兩個長度不同的列表
例如
A = [1,2,3,4,5,6,7,8,9]
B = ["A","B","C"]
我期待這個
[(1, 'A'), (2, 'B'), (3, 'C'), (4, 'A'), (5, 'B'), (6, 'C'), (7, 'A'), (8, 'B'), (9, 'C')]
但是內置的zip
不會重復與更大的列表配對。 是否存在任何內置方法可以實現這一目標?
這是我的代碼:
idx = 0
zip_list = []
for value in larger:
zip_list.append((value,smaller[idx]))
idx += 1
if idx == len(smaller):
idx = 0
您可以使用itertools.cycle
:
使迭代器從可迭代對象返回元素並保存每個元素的副本。 當迭代用完時,從保存的副本中返回元素。 無限重復。
例子:
A = [1,2,3,4,5,6,7,8,9]
B = ["A","B","C"]
from itertools import cycle
zip_list = zip(A, cycle(B)) if len(A) > len(B) else zip(cycle(A), B)
嘗試這個。
A = [1,2,3,4,5,6,7,8,9]
B = ["A","B","C"]
Z = []
for i, a in enumerate(A):
Z.append((a, B[i % len(B)]))
只需確保較大的列表在A
。
您可以使用itertools.cycle
:
from itertools import cycle
my_list = [1, 2, 3, 5, 5, 9]
another_list = ['Yes', 'No']
cyc = cycle(another_list)
print([[i, next(cyc)] for i in my_list])
# [[1, 'Yes'], [2, 'No'], [3, 'Yes'], [5, 'No'], [5, 'Yes'], [9, 'No']]
你知道第二個名單更短嗎?
import itertools
list(zip(my_list, itertools.cycle(another_list)))
這實際上會給你一個元組列表而不是一個列表列表。 我希望沒關系。
任意數量的迭代的解決方案,你不知道哪個最長(也允許任何空迭代的默認值):
from itertools import cycle, zip_longest
def zip_cycle(*iterables, empty_default=None):
cycles = [cycle(i) for i in iterables]
for _ in zip_longest(*iterables):
yield tuple(next(i, empty_default) for i in cycles)
for i in zip_cycle(range(2), range(5), ['a', 'b', 'c'], []):
print(i)
輸出:
(0, 0, 'a', None)
(1, 1, 'b', None)
(0, 2, 'c', None)
(1, 3, 'a', None)
(0, 4, 'b', None)
像這樣嘗試:
my_list=[ 1, 2, 3, 5, 5, 9]
another_list=['Yes','No']
if type(len(my_list)/2) == float:
ml=int(len(my_list)/2)+1
else:
ml=int(len(my_list)/2)
print([[x,y] for x,y in zip(my_list,another_list*ml)])
原生方式:
zip()
進行迭代一個非常簡單的方法是將短列表相乘,使其更長:
my_list = [1, 2, 3, 5, 5, 9]
another_list = ['Yes', 'No']
zip(my_list, another_list*3))
#[(1, 'Yes'), (2, 'No'), (3, 'Yes'), (5, 'No'), (5, 'Yes'), (9, 'No')]
請注意,這里不需要仔細計算乘數,因為zip
只會到達最短列表的長度(乘數的重點是確保最短列表是my_list
)。 也就是說,如果使用100
而不是3
,結果將是相同的。
您可以在計數的循環中使用模%
運算符
my_list=[1, 2, 3, 5, 5, 9]
another_list=['Yes','No']
new_list = []
for cur in range(len(my_list)):
new_list.append([my_list[cur], another_list[cur % 2]])
# [[1, 'Yes'], [2, 'No'], [3, 'Yes'], [5, 'No'], [5, 'Yes'], [9, 'No']]
2
可以替換為len(another_list)
對稱,無條件單行
[*zip(A*(len(B)//len(A) + 1), B*(len(A)//len(B) + 1))]
嚴格回答“如何壓縮兩個不同大小的列表?”
需要一個相同大小列表的補丁才能通用:
[*(zip(A, B) if len(A) == len(B)
else zip(A*(len(B)//len(A) + 1),
B*(len(A)//len(B) + 1)))]
讓我們使用np.tile
和zip
:
my_list = [1, 2, 3, 5, 5, 9]
another_list = ['Yes', 'No']
list(zip(my_list,np.tile(another_list, len(my_list)//len(another_list) + 1)) )
輸出:
[(1, 'Yes'), (2, 'No'), (3, 'Yes'), (5, 'No'), (5, 'Yes'), (9, 'No')]
我喜歡Henry Yik 的答案,而且執行速度要快一些,但這是一個不使用 itertools 的答案。
my_list = [1, 2, 3, 5, 5, 9]
another_list = ['Yes', 'No']
new_list = []
for i in range(len(my_list)):
new_list.append([my_list[i], another_list[i % len(another_list)]])
new_list
[[1, 'Yes'], [2, 'No'], [3, 'Yes'], [5, 'No'], [5, 'Yes'], [9, 'No']]
可能有更好的方法,但您可以制作一個函數,將您的列表重復到您想要的任何長度。
def repeatlist(l,i):
'''give a list and a total length'''
while len(l) < i:
l += l
while len(l) > i:
l.pop()
然后做
repeatlist(B,len(A))
zip_list = zip(A,B)
對於以任何順序處理任何有限數量的潛在無限迭代的版本:
from itertools import cycle, tee, zip_longest
def cyclical_zip(*iterables):
iterables_1, iterables_2 = zip(*map(tee, iterables)) # Allow proper iteration of iterators
for _, x in zip(
zip_longest(*iterables_1), # Limit by the length of the longest iterable
zip(*map(cycle, iterables_2))): # the cycling
yield x
assert list(cyclical_zip([1, 2, 3], 'abcd', 'xy')) == [(1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'x'), (1, 'd', 'y')] # An example and test case
這個問題試圖以錯誤的方式解決問題。
您在這里嘗試的是獲取兩個給定列表的元素的所有可能排列。 這可以使用itertools.product
輕松實現
>>> from itertools import product
>>> list(product(range(2), range(5)))
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4)]
>>> list(product(range(2), range(2)))
[(0, 0), (0, 1), (1, 0), (1, 1)]
>>> list(product(range(2), range(2), range(3)))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 1, 2), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 1, 0), (1, 1, 1), (1, 1, 2)]
現在有了列表理解
[(i, B[i % 3 - 1]) for i in A]
或者如果A
的元素不是連續的並且不擔心列表長度
[(j, B[i % len(B)]) for i, j in enumerate(A)] if len(A) >= len(B) else \
[(A[i % len(A)], j) for i, j in enumerate(B)]
d1=['one','two','three']
d2=[1,2,3,4,5]
zip(d1,d2)
<zip object at 0x05E494B8>
list(zip(d1,d2))
{'one': 1, 'two': 2, 'three': 3}
注意:Python 3.7+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.