Подтвердить что ты не робот

Перечисление и заполнение списка Python

У меня есть список списков, так что длина каждого внутреннего списка равна 1 или n (предположим n > 1).

>>> uneven = [[1], [47, 17, 2, 3], [3], [12, 5, 75, 33]]

Я хочу перенести список, но вместо того, чтобы обрезать более длинный список (как и zip) или заполнить более короткие списки None, я хочу заполнить более короткие списки своим собственным значением. Другими словами, я хотел бы получить:

>>> [(1, 47, 3, 12), (1, 17, 3, 5), (1, 2, 3, 75), (1, 3, 3, 33)]

Я могу сделать это с помощью нескольких итераций:

>>> maxlist = len(max(*uneven, key=len))
>>> maxlist
4
>>> from itertools import repeat
>>> uneven2 = [x if len(x) == maxlist else repeat(x[0], maxlist) for x in uneven]
>>> uneven2
[[1, 1, 1, 1], [47, 17, 2, 3], [3, 3, 3, 3], [12, 5, 75, 33]]
>>> zip(*uneven2)
[(1, 47, 3, 12), (1, 17, 3, 5), (1, 2, 3, 75), (1, 3, 3, 33)]

Но есть ли лучший подход? Должен ли я действительно знать maxlist заранее, чтобы выполнить это?

4b9b3361

Ответ 1

Вы можете повторить один список элементов навсегда:

uneven = [[1], [47, 17, 2, 3], [3], [12, 5, 75, 33]]

from itertools import repeat

print zip(*(repeat(*x) if len(x)==1 else x for x in uneven))

Ответ 2

Вместо itertools.cycle() можно использовать

>>> from itertools import cycle
>>> uneven3 = [x if len(x) != 1 else cycle(x) for x in uneven]
>>> zip(*uneven3)
[(1, 47, 3, 12), (1, 17, 3, 5), (1, 2, 3, 75), (1, 3, 3, 33)]

Это означает, что вам не нужно заранее знать maxlist.

Ответ 3

Мне очень понравилась идея @chris-morgan моделировать itertools.izip_longest, поэтому, когда я наконец получил вдохновение, я написал функцию izip_cycle.

def izip_cycle(*iterables, **kwargs):
    """Make an iterator that aggregates elements from each of the iterables.
    If the iterables are of uneven length, missing values are filled-in by cycling the shorter iterables.
    If an iterable is empty, missing values are fillvalue or None if not specified.
    Iteration continues until the longest iterable is exhausted.
    """
    fillvalue = kwargs.get('fillvalue')
    counter = [len(iterables)]
    def cyclemost(iterable):
        """Cycle the given iterable like itertools.cycle, unless the counter has run out."""
        itb = iter(iterable)
        saved = []
        try:
            while True:
                element = itb.next()
                yield element
                saved.append(element)
        except StopIteration:
            counter[0] -= 1
            if counter[0] > 0:
                saved = saved or [fillvalue]
                while saved:
                    for element in saved:
                        yield element
    iterators = [cyclemost(iterable) for iterable in iterables]
    while iterators:
        yield tuple([next(iterator) for iterator in iterators])

print list(izip_cycle([], range(3), range(6), fillvalue='@'))