如何压缩两个不同大小的列表?

问题描述 投票:31回答:8

我想压缩两个不同长度的列表

例如

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
python list python-3.x
8个回答
63
投票

你可以使用itertools.cycle

使迭代器返回迭代中的元素并保存每个元素的副本。当iterable耗尽时,返回保存副本中的元素。无限期地重复。

例:

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)

7
投票

尝试这个。

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中。


1
投票

对称的,没有条件的一个班轮

[*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)))]

0
投票

可能有更好的方法,但您可以创建一个函数,将您的列表重复到您想要的任何长度。

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)

0
投票

对于以任何顺序使用任何有限数量的潜在无限可迭代的版本:

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

0
投票

任意数量的迭代的解决方案,并且您不知道哪个是最长的(也允许任何空迭代的默认值):

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)

-1
投票

而现在与list comprehentions

[(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)]

-1
投票
d1=['one','two','three']
d2=[1,2,3,4,5]

Zip

zip(d1,d2)
<zip object at 0x05E494B8>

list of zip

list(zip(d1,d2))

dictionary of list of zip

{'one': 1, 'two': 2, 'three': 3}

注意:Python 3.7+

© www.soinside.com 2019 - 2024. All rights reserved.