相关疑难解决方法(0)

具有重复值和后缀的列表

我有一个清单,a:

a = ['a','b','c']
Run Code Online (Sandbox Code Playgroud)

并且需要复制一些带有以_ind这种方式添加的后缀的值(顺序很重要):

['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
Run Code Online (Sandbox Code Playgroud)

我试过了:

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']
Run Code Online (Sandbox Code Playgroud)

我认为我的解决方案有点过于复杂.有没有更好,更pythonic的解决方案?

python list-comprehension list suffix

39
推荐指数
5
解决办法
1737
查看次数

标签 统计

list ×1

list-comprehension ×1

python ×1

suffix ×1