Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有重复值和后缀的列表_Python_List_List Comprehension_Suffix - Fatal编程技术网

Python 具有重复值和后缀的列表

Python 具有重复值和后缀的列表,python,list,list-comprehension,suffix,Python,List,List Comprehension,Suffix,我有一张清单,a: a = ['a','b','c'] 并且需要复制一些以这种方式添加后缀\u ind的值(顺序很重要): 我试过: b = [[x, x + '_ind'] for x in a] c = [item for sublist in b for item in sublist] print (c) ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind'] 是否有更好、更具python风格的解决方案?通过将选项移动到列表中的内部for循环,可以将其缩

我有一张清单,
a

a = ['a','b','c']
并且需要复制一些以这种方式添加后缀
\u ind
的值(顺序很重要):

我试过:

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

是否有更好、更具python风格的解决方案?

通过将选项移动到列表中的内部for循环,可以将其缩短一点:

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

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

你可以把它做成发电机:

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']
您也可以使用
itertools.product
itertools.starmap
itertools.chain
或嵌套理解来实现,但在大多数情况下,我更喜欢简单易懂的自定义生成器函数


使用python3.3,您还可以使用
yield from
-generator delegation使这个优雅的解决方案更加简洁:

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

您可以使用
itertools.chain()

输出:

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

另一种选择是剪接(Python2.x,3.x):


在列表理解和生成器发明/普及之前,人们通常认为更简单:


*我并不是说那些构造/工具是邪恶的,只是想指出有一个简单的解决方案。

既然你要求“简单”,我想我会加入这个(尽管可能不是
pythonic
方式):


我会用。这样,您就不需要使用
*
Alt:
list(itertools.chain(*zip(a,[x+''ind'表示a中的x]))
list(itertools.chain(*zip(a,map(lambda x:x+'ind',a))
@cᴏʟᴅsᴘᴇᴇᴅ 同意,但是再一次,
chain.from\u iterable
会更干净一点:)
list(chain.from\u iterable(zip(a,[i+''ind'代表a中的i]))
。并不是说它特别重要。为了记录在案,这个解决方案没有错。我可以推荐一个列表理解版本吗<代码>列表([((收益率x),(收益率(x+')))(对于a中的x]);[a',a_ind',b',b_ind',c',c_ind']@cᴏʟᴅsᴘᴇᴇᴅ 世界跆拳道联盟。。。从未见过这样的“内联”收益率。再多玩一点,这也行得通:
list([(从(x,x+''u ind')中的x的收益率])
@StefanPochmann Killer。想发布一个答案吗?如果没有,我将编辑我的:p@StefanPochmann只有py3k。他们增加了对列表组件的收益率支持,以允许创建生成器。@idjaw哦,等等,刚才在我的IDE上也看到了红色下划线。xD“仅适用于Python3.x”-实际上它仅适用于Python3.3及更高版本。见@ChristianDean谢谢。添加了链接。@Bergi是的,因为带有yield的列表comp返回一个生成器。如果您想混淆Python代码,此选项最有用。@cᴏʟᴅsᴘᴇᴇᴅ 不,对于代码,Psidom的答案获胜。:)据我所知,理解和生成器的发明(实现)是为了使这些“构造和工具”更简单(而且肯定要快得多)。但我想这是一个相当基于意见的声明。
import itertools

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

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

print new_list
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
result = [None] * len(a) * 2
result[::2], result[1::2] = a, map(lambda x: x + '_ind', a)

result
# ['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
>>> a = ['a', 'b', 'c']
>>> b = []
>>> for x in a: b.extend([x, x+'_ind'])
... 
>>> b
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
for i in mylist: 
    mylist1.append(i);
    mylist1.append(i + '_ind');