我有一个字符串列表,需要将其转换为较小的字符串列表,具体取决于两个连续元素是否属于同一短语。 目前,如果第 i 个字符串的最后一个字符较低且第 i+1 个字符串的第一个字符也较低,则会发生这种情况,但是将来应该检查更复杂的条件。
例如这段非常深刻的文字:
['I am a boy',
'and like to play'
'My friends also'
'like to play'
'Cats and dogs are '
'nice pets, and'
'we like to play with them'
]
应该变成:
['I am a boy and like to play',
'My friends also like to play',
'Cats and dogs are nice pets, and we like to play with them'
]
我的Python解决方案
最佳答案
我认为您发布的数据是用逗号分隔的。如果是pfb则简单的循环解决方案。
data=['I am a boy',
'and like to play',
'My friends also',
'like to play',
'Cats and dogs are ',
'nice pets, and',
'we like to play with them'
]
required_list=[]
for j,i in enumerate(data):
print(i,j)
if j==0:
req=i
else:
if i[0].isupper():
required_list.append(req)
req=i
else:
req=req+" "+i
required_list.append(req)
print(required_list)
关于python - 在Python中递归地合并字符串列表中的连续元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62287077/