Python 如果项目不';t以“结束”&引用;
我有一个字符串列表,如果我的项目没有以“.”结尾,我想连接它们Python 如果项目不';t以“结束”&引用;,python,Python,我有一个字符串列表,如果我的项目没有以“.”结尾,我想连接它们 my_list=[“这是我的第一个字符串”,“这是我的第二个字符串”,“这是我的第二个字符串的中间”,“这是我的第二个字符串的结尾。”] 对于范围内的索引(len(my_列表)): text=我的列表[索引]: 如果不是text.endswith(“.”),则: 期望 [“这是我的第一个字符串。”,“这是我的第二个字符串,这是我的第二个字符串的中间,这是我的第二个字符串的结尾”这是一种使用str.join和str.split的方法
my_list=[“这是我的第一个字符串”,“这是我的第二个字符串”,“这是我的第二个字符串的中间”,“这是我的第二个字符串的结尾。”]
对于范围内的索引(len(my_列表)):
text=我的列表[索引]:
如果不是text.endswith(“.”),则:
期望
[“这是我的第一个字符串。”,“这是我的第二个字符串,这是我的第二个字符串的中间,这是我的第二个字符串的结尾”
这是一种使用str.join
和str.split
的方法
Ex:
my_list=["This is my first string.","This is my second string, ","this is the middle of my second string","and this is the end of my second string."]
result = [i.strip() + "." for i in " ".join(my_list).strip().split(".") if i]
print(result)
['This is my first string.',
'This is my second string, this is the middle of my second string and this is the end of my second string.']
输出:
my_list=["This is my first string.","This is my second string, ","this is the middle of my second string","and this is the end of my second string."]
result = [i.strip() + "." for i in " ".join(my_list).strip().split(".") if i]
print(result)
['This is my first string.',
'This is my second string, this is the middle of my second string and this is the end of my second string.']
如果
”,请在之后发布其余代码。加入(我的\u列表)。拆分(“。”
?可能缺少空格。”。加入([s.strip()代表我的\u列表中的s])。拆分(“。”)
?嗯,我的建议在“This…”前面加了一个空格-也不完全正确str.strip
应该解决在字符串之间加了两个空格的问题:string,这个
-比我想象的要复杂…非常干净:result=[I.strip()+”,表示我在(“.”。join([s.strip(),表示我的_列表中的s])。split(“.”如果我的话。