简体   繁体   English

Python以特定方式附加列表

[英]Python append lists in a specific way

I know that I can practically merge two list (in Python 2.7) as follows 我知道我可以合并两个列表(在Python 2.7中),如下所示

list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
merged = list1 + list2
print merged
# ['one', 'two', 'three', 'four', 'five', 'A', 'B', 'C', 'D', 'E']

The question is, I would like one of list2 inserted after every two of list1. 问题是,我想将list2之一插入到list1的每两个之后。 Example: 例:

list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
after 2 of list1:
     add 1 of list2
print merged
# ['one', 'two', 'A', 'three', 'four', 'B', 'five', 'six', 'C', 'seven', 'eight', 'D', 'nine', 'ten']

Any help would be really appreciated! 任何帮助将非常感激!

This is the kind of case where using a raw iterator makes for clean code, you can call next on an iterator to get the next value and then append it to the result so the list creation is quite intuitive: 在这种情况下,使用原始迭代器可以生成干净的代码,您可以在迭代器上调用next来获取下一个值,然后将其附加到结果中,从而使列表创建非常直观:

list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
iter_list1 = iter(list1)
iter_list2 = iter(list2)

final = []
try: #broken when one of the iterators runs out (and StopIteration is raised)
    while True:
        final.append(next(iter_list1))
        final.append(next(iter_list1))

        final.append(next(iter_list2))
except StopIteration:
    pass
#one will already be empty, add the remaining elements of the non-empty one to the end of the list.
final.extend(iter_list1)
final.extend(iter_list2)

print(final)

You can try izip_longest for python 2.7 (or zip_longest for python 3+), assuming extra elements from either of the lists will be appended to the result: 您可以尝试izip_longest为Python 2.7(或zip_longest对于蟒蛇3+),假设从任一列表中多余的元素将被追加到的结果:

from itertools import izip_longest

[y for x in izip_longest(list1[::2], list1[1::2], list2) for y in x if y is not None]
# ['one', 'two', 'A', 'three', 'four', 'B', 'five', 'C', 'D', 'E']

Or use zip if you want to drop unpaired elements: 如果要删除未配对的元素,请使用zip

[y for x in zip(list1[::2], list1[1::2], list2) for y in x]
# ['one', 'two', 'A', 'three', 'four', 'B']

You could use enumerate and list.insert : 您可以使用enumeratelist.insert

>>> l1 = ['A', 'B', 'C', 'D']
>>> l2 = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
>>> l3 = l2[:]  # makes a copy
>>> for idx, item in enumerate(l1):
...     l3.insert((idx*3+2), item)
>>> l3
['one', 'two', 'A', 'three', 'four', 'B', 'five', 'six', 'C', 'seven', 'eight', 'D', 'nine', 'ten']
>>> from operator import add
>>> reduce(add,zip(list1[::2], list1[1::2], list2))
('one', 'two', 'A', 'three', 'four', 'B')

Caution - This will drop the trailing elements. 注意-这将删除尾随元素。

Explanation: 说明:

you can use list slicing like l as list l[low:high:step] to get, 您可以像l一样使用列表切片作为列表l [low:high:step]来获得,

>>> list1[::2]
['one', 'three', 'five']
>>> list1[1::2]
['two', 'four']

With that, 接着就,随即,

>>> zip(list1[::2], list1[1::2])
[('one', 'two'), ('three', 'four')]

Therefore, 因此,

>>> zip(list1[::2], list1[1::2], list2)
[('one', 'two', 'A'), ('three', 'four', 'B')]
>>> reduce(add,zip(list1[::2], list1[1::2], list2))
('one', 'two', 'A', 'three', 'four', 'B')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM