Python:如何通过被另一个字符串列表拆分的子字符串创建列表?

Python: How to create a list by substrings there was splitted by another list of strings?

我需要创建一个列表,该列表将由为另一个字符串列表提取的子字符串组成。我尝试了“for-loop”代码,但得到了意想不到的结果。

例如:

description = ['How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
 'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom',]

descrition_splitted = []

for i in description:
  des = (i)
  descrition_splitted.append((des.split(',')))

description_splitted 列表的长度只有 2 个元素,而不是 9 个(子串数)。好像每个元素包含很多子串。

我做错了什么?

是否需要另一个“for-loop”?

您可以使用 .extend 而不是 .append

descrition_splitted.extend((i.split(',')))

或者您可以再次循环 , 上的拆分结果以获得 9 个结果。

description = ['How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
               'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom',]

descrition_splitted = []

for i in description:
    for part in i.split(','):
        descrition_splitted.append(part)

print(len(descrition_splitted))

输出

9

您可以使用 extend 而不是 append。这与评论中提到的 += 相同。

description = [
    'How g... and focus',
    'The s... chosen idiom'
]

comma_fragments = []

for segment in description:
  comma_fragments.extend(segment.split(','))

如果你想尝试列表理解,你甚至可以这样做

comma_fragments = [s for d in description for s in d.split(',')]

例如

>>> description = ['a,b', 'c,d,e']
>>> [s for d in description for s in d.split(',')]
['a', 'b', 'c', 'd', 'e']

如果我猜对了你的目的,你需要使用 +=(列表 + 列表或用列表扩展列表)而不是 append(将项目插入列表)

    descrition_splitted += des.split(',')

顺便说一句,我们可以将您的代码重构为:

description = [
    'How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
    'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom', ]

descrition_splitted = []

for des in description:
    descrition_splitted += des.split(',')

更短:

description = [
    'How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
    'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom', ]

descrition_splitted = []
list(map(descrition_splitted.extend, (des.split(',') for des in description)))

试试这个

descrition_splitted=[]
for i in description:
  descrition_splitted=descrition_splitted+i.split(',')
len(descrition_splitted)

输出

9