Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何通过子字符串创建一个列表被另一个字符串列表分割?_Python_String_List_For Loop_Substring - Fatal编程技术网

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

Python:如何通过子字符串创建一个列表被另一个字符串列表分割?,python,string,list,for-loop,substring,Python,String,List,For Loop,Substring,我需要创建一个列表,该列表将由为另一个字符串列表提取的子字符串组成。我尝试了一个“for循环”代码,但得到了意想不到的结果 例如: 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 a

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

例如:

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 list的长度仅为2个元素,而不是9个(子字符串的数量)。就像每个元素都包含许多子字符串一样

我做错了什么


需要另一个“for循环”吗?

您可以使用
.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

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(',')]
e、 g


您可以使用
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(',')]
e、 g


如果我猜您的目的是正确的,您需要使用
+=
(列表+列表或用列表扩展列表)而不是
附加
(将项目插入列表)

description_splitted+=des.split(','))
顺便说一句,我们可以将您的代码重构为:

说明=[
“一次糟糕的旅行能有多好……天使,坏业力能召唤出……令人印象深刻的声音,在乐队……专辑,2017年的死亡之歌中,他们的喜怒无常……攻击以令人印象深刻的……和专注的方式执行”,
“西塔人系着两个……舒适地坐在……鸽子的绝望”旁边,证明了多样性……从他们选择的习语中挤出来,]
description_splitted=[]
对于描述中的des:
描述_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)))

如果我猜您的目的是正确的,您需要使用
+=
(列表+列表或用列表扩展列表)而不是
附加
(将项目插入列表)

description_splitted+=des.split(','))
顺便说一句,我们可以将您的代码重构为:

说明=[
“一次糟糕的旅行能有多好……天使,坏业力能召唤出……令人印象深刻的声音,在乐队……专辑,2017年的死亡之歌中,他们的喜怒无常……攻击以令人印象深刻的……和专注的方式执行”,
“西塔人系着两个……舒适地坐在……鸽子的绝望”旁边,证明了多样性……从他们选择的习语中挤出来,]
description_splitted=[]
对于描述中的des:
描述_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
试试这个

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

9

这里实际需要的输出是什么?使用
+=
而不是append<代码>描述\u splitted+=des.split(',')Hi@TimBiegeleisen我想要一个包含9个元素的列表。每个子字符串必须是最终列表的一个元素。例如,如果我使用len(description_splitted),它将显示9而不是2.OMG@TuanChau非常感谢@别担心。我添加了一个解释和重构代码的答案。您实际需要的输出是什么?使用
+=
而不是append<代码>描述\u splitted+=des.split(',')Hi@TimBiegeleisen我想要一个包含9个元素的列表。每个子字符串必须是最终列表的一个元素。例如,如果我使用len(description_splitted),它将显示9而不是2.OMG@TuanChau非常感谢@别担心。我添加了一个解释和重构代码的答案。我鼓励您使用较长的版本,我的较短版本只是为了演示。这不利于理解代码在这个阶段的工作方式。我鼓励您使用较长的版本,我的较短版本只是为了演示。这不利于理解代码在此阶段的工作方式。