用另一个列表的内容替换列表项

时间:2013-02-19 16:49:45

标签: python list-comprehension

this question类似,但我不想用一个项目替换另一个项目,而是想用列表内容替换任何一个项目。

orig = [ 'a', 'b', 'c', 'd', 'c' ]
repl = [ 'x', 'y', 'z' ]
desired = [ 'a', 'b', 'x', 'y', 'z', 'd', 'x', 'y', 'z' ]

# these are all incorrect, or fail to compile
[ repl if x == 'c' else x for x in orig ]
[ [a for a in orig] if x == 'c' else x for x in orig ]
[ (a for a in orig) if x == 'c' else x for x in orig ]
[ a for a in orig if x == 'c' else x for x in orig ]

编辑:明确表示我打算替换所有项目的出现次数,而不仅仅是第一次出现。 (向在答案中没有涉及该案件的任何人道歉。)

5 个答案:

答案 0 :(得分:6)

>>> orig = [ 'a', 'b', 'c', 'd' ]
>>> repl = [ 'x', 'y', 'z' ]
>>> desired = list(orig)  #can skip this and just use `orig` if you don't mind modifying it (and it is a list already)
>>> desired[2:3] = repl
>>> desired
['a', 'b', 'x', 'y', 'z', 'd']

当然,如果您不知道'c'位于索引2,则可以使用orig.index('c')来查找该信息。

答案 1 :(得分:4)

不同的方法:当我在做替换时,我更愿意用字典来思考。所以我会做像

这样的事情
>>> orig = [ 'a', 'b', 'c', 'd' ]
>>> rep = {'c': ['x', 'y', 'z']}
>>> [i for c in orig for i in rep.get(c, [c])]
['a', 'b', 'x', 'y', 'z', 'd']

其中最后一行是标准的扁平化习语。

这种方法的一个优点(缺点?)是它会处理'c'的多次出现。

[更新:]

或者,如果您愿意:

>>> from itertools import chain
>>> list(chain.from_iterable(rep.get(c, [c]) for c in orig))
['a', 'b', 'x', 'y', 'z', 'd']

在修订后的测试案例中:

>>> orig = [ 'a', 'b', 'c', 'd', 'c' ]
>>> rep = {'c': ['x', 'y', 'z']}
>>> list(chain.from_iterable(rep.get(c, [c]) for c in orig))
['a', 'b', 'x', 'y', 'z', 'd', 'x', 'y', 'z']

答案 2 :(得分:2)

不需要任何花哨的东西:

desired = orig[:2] + repl + orig[3:]

要查找2,您可以搜索orig.index('c')

x = orig.index('c')
desired = orig[:x] + repl + orig[x+1:]

如果repl不是列表,只需使用list(repl)

答案 3 :(得分:0)

如果向后枚举,您可以随时扩展列表,因为您移动的项目已经完成了枚举。

>>> orig = [ 'a', 'b', 'c', 'd', 'c' ]
>>> repl = [ 'x', 'y', 'z' ]
>>> desired = [ 'a', 'b', 'x', 'y', 'z', 'd', 'x', 'y', 'z' ]
>>> for i in xrange(len(orig)-1, -1, -1):
...     if orig[i] == 'c':
...             orig[i:i+1] = repl
... 
>>> orig
['a', 'b', 'x', 'y', 'z', 'd', 'x', 'y', 'z']

答案 4 :(得分:0)

另一种方式:

>>> import operator
>>> orig = [ 'a', 'b', 'c', 'd', 'c' ]
>>> repl = [ 'x', 'y', 'z' ]
>>> output = [repl if x == 'c' else [x] for x in orig]
>>> reduce(operator.add, output)
['a', 'b', 'x', 'y', 'z', 'd', 'x', 'y', 'z']
>>> 
相关问题