How does one merge a list, but maintain the previous lists order?

Deadly 提交于 2021-01-28 03:03:05

问题


I have this list once merged using the below gives me this output.

['description t2_HELP', 'description t2_1507', 'description t2_1189', 'description t2_5625', 'description None', 'description None', 'description None', 'description None', 'interface Gi3/0/13', 'interface Gi3/0/7', 'interface Gi1/0/11', 'interface Gi3/0/41']

I however want to maintain the order of that data when I merge it in. So example list set would look like this.

['interface Gi3/0/25','description None','description t2_2696','interface Gi1/0/29','description None','description t2_4148','interface Gi1/0/31','description None','description t2_4212','interface Gi2/0/31','description None','description t2_4271']

Here is how i'm merging the lists,

joinlist = data1 + data2 + data3

the lists look something like this

data1 = ['interface Gi3/0/25','interface Gi1/0/29','interface Gi1/0/31','interface Gi2/0/31']
data2 = ['description None','description None','description None','description None']
data3 = ['description t2_2696','description t2_4148','description t2_4212','description t2_4271']

回答1:


The other suggestions are fine if you have equal length lists (zip) or don't mind filler values in the result (zip_longest).

But logically what you're asking for here is a round robining of inputs, not a zipping. By flattening, you lose the paired-up aspect of zip, and can get bitten by it for uneven length inputs.

A more general solution to this problem is available with the itertools module's roundrobin recipe (click the link for code, I'm just including usage here):

joinlist = list(roundrobin(data1, data2, data3))

That takes an element from each of the inputs in order; if one input is shorter than the others, it is omitted from later rounds without injecting garbage filler values or losing values from the longer input(s).




回答2:


Use built-in zip if you are okay with any longer list get truncated:

joinlist = []
for d in zip(data1,data2,data3):
    joinlist.extend(d)

Output:

['interface Gi3/0/25',
 'description None',
 'description t2_2696',
 'interface Gi1/0/29',
 'description None',
 'description t2_4148',
 'interface Gi1/0/31',
 'description None',
 'description t2_4212',
 'interface Gi2/0/31',
 'description None',
 'description t2_4271']

If you want to avoid the truncate, use itertools.zip_longest:

data2.append('test_me') # For testing purpose
joinlist = []
for d in zip_longest(data1,data2,data3):
    joinlist.extend(d)

Output:

['interface Gi3/0/25',
 'description None',
 'description t2_2696',
 'interface Gi1/0/29',
 'description None',
 'description t2_4148',
 'interface Gi1/0/31',
 'description None',
 'description t2_4212',
 'interface Gi2/0/31',
 'description None',
 'description t2_4271',
 None,
 'a',
 None]

Note: None at the end of the joinlist is because the zip_longest, by default, zips shorter iterable(s) with None. You can set any default value using fillvalue of zip_longest.




回答3:


You can use a combination of list comprehensions and zip:

>>> [i for t in zip(data1, data2, data3) for i in t]
['interface Gi3/0/25', 'description None', 'description t2_2696', 'interface Gi1/0/29', 'description None', 'description t2_4148', 'interface Gi1/0/31', 'description None', 'description t2_4212', 'interface Gi2/0/31', 'description None', 'description t2_4271']


来源:https://stackoverflow.com/questions/56553332/how-does-one-merge-a-list-but-maintain-the-previous-lists-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!