Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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 如果项目不';t以“结束”&引用;_Python - Fatal编程技术网

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(“.”如果我的话。