Pythonic way to combine two lists in an alternating fashion?

前端 未结 21 3090
误落风尘
误落风尘 2020-11-22 16:13

I have two lists, the first of which is guaranteed to contain exactly one more item than the second. I would like to know the most Pythonic way to create a

相关标签:
21条回答
  • 2020-11-22 16:58

    Here's a one liner using list comprehensions, w/o other libraries:

    list3 = [sub[i] for i in range(len(list2)) for sub in [list1, list2]] + [list1[-1]]
    

    Here is another approach, if you allow alteration of your initial list1 by side effect:

    [list1.insert((i+1)*2-1, list2[i]) for i in range(len(list2))]
    
    0 讨论(0)
  • 2020-11-22 17:00

    This is nasty but works no matter the size of the lists:

    list3 = [element for element in list(itertools.chain.from_iterable([val for val in itertools.izip_longest(list1, list2)])) if element != None]
    
    0 讨论(0)
  • 2020-11-22 17:01

    There's a recipe for this in the itertools documentation:

    from itertools import cycle, islice
    
    def roundrobin(*iterables):
        "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
        # Recipe credited to George Sakkis
        pending = len(iterables)
        nexts = cycle(iter(it).next for it in iterables)
        while pending:
            try:
                for next in nexts:
                    yield next()
            except StopIteration:
                pending -= 1
                nexts = cycle(islice(nexts, pending))
    

    EDIT:

    For python's version greater than 3:

    from itertools import cycle, islice
    
    def roundrobin(*iterables):
        "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
        # Recipe credited to George Sakkis
        pending = len(iterables)
        nexts = cycle(iter(it).__next__ for it in iterables)
        while pending:
            try:
                for next in nexts:
                    yield next()
            except StopIteration:
                pending -= 1
                nexts = cycle(islice(nexts, pending))
    
    0 讨论(0)
  • 2020-11-22 17:01

    If both lists have equal length, you can do:

    [x for y in zip(list1, list2) for x in y]
    

    As the first list has one more element, you can add it post hoc:

    [x for y in zip(list1, list2) for x in y] + [list1[-1]]
    
    0 讨论(0)
  • 2020-11-22 17:01

    I'd do the simple:

    chain.from_iterable( izip( list1, list2 ) )
    

    It'll come up with an iterator without creating any additional storage needs.

    0 讨论(0)
  • 2020-11-22 17:02
    import itertools
    print [x for x in itertools.chain.from_iterable(itertools.izip_longest(list1,list2)) if x]
    

    I think this is the most pythonic way of doing it.

    0 讨论(0)
提交回复
热议问题