Merge some list items in a Python List

后端 未结 5 2068
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 01:42

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]
         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 01:53

    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.

提交回复
热议问题