合并Python列表中的一些列表项 [英] Merge some list items in a Python List
本文介绍了合并Python列表中的一些列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个这样的列表:
Say I have a list like this:
[a, b, c, d, e, f, g]
如何修改该列表,使其看起来像这样?
How do modify that list so that it looks like this?
[a, b, c, def, g]
我更希望它直接修改现有列表,而不创建新列表.
I would much prefer that it modified the existing list directly, not created a new list.
推荐答案
合并应在什么基础上进行?您的问题相当模糊.另外,我假设a,b,...,f应该是字符串,即'a','b',...,'f'.
On what basis should the merging take place? Your question is rather vague. Also, I assume a, b, ..., f are supposed to be strings, that is, 'a', 'b', ..., 'f'.
>>> x = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> x[3:6] = [''.join(x[3:6])]
>>> x
['a', 'b', 'c', 'def', 'g']
Check out the documentation on sequence types, specifically on mutable sequence types. And perhaps also on string methods.
这篇关于合并Python列表中的一些列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文