List with duplicated values and suffix

我们两清 提交于 2019-11-27 07:34:02
MSeifert

You could make it a generator:

def mygen(lst):
    for item in lst:
        yield item
        yield item + '_ind'

>>> a = ['a','b','c']
>>> list(mygen(a))
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

You could also do it with itertools.product, itertools.starmap or itertools.chain or nested comprehensions but in most cases I would prefer a simple to understand, custom generator-function.


With python3.3, you can also use yield from—generator delegation—to make this elegant solution just a bit more concise:

def mygen(lst):
    for item in lst:
        yield from (item, item + '_ind')

It can be shortened a little bit by moving the options to the inner for loop in the list comprehension:

a = ['a','b','c']

[item for x in a for item in (x, x + '_ind')]
# ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

Another alternative with splicing (Python2.x, 3.x):

In [642]: result = [None] * len(a) * 2

In [643]: result[::2], result[1::2] = a, map(lambda x: x + '_ind', a)

In [644]: result
Out[644]: ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

You can use itertools.chain():

import itertools

l = ['a','b','c']

new_list = list(itertools.chain.from_iterable([[i, i+"_ind"] for i in l]))

print new_list

Output:

['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

Before list comprehensions and generators were invented/became widespread, people used to think much simpler1:

>>> a = ['a', 'b', 'c']
>>> b = []
>>> for x in a: b.extend([x, x+'_ind'])
... 
>>> b
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']

* I don't mean that those constructs/tools are evil, just wanted to point out that there is a simple solution.

Eladian

Since you asked for "simple", I thought I'd throw this in (albeit, maybe not the pythonic way):

for i in mylist: 
    mylist1.append(i);
    mylist1.append(i + '_ind');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!