试图弄清楚为什么将空字符串附加到列表中

时间:2019-06-26 13:16:02

标签: python list

我的程序将比较两个段落并在列表中返回相似的行。因此,将列表中的每一行拆分并进行比较。类似的行将追加到列表中。但是,结果包括一个空字符串。请帮助我弄清楚它的来源。

story1 = '''This is a story.
This has multiple lines.
All lines will be split.
This is the last line.
'''

story2 = '''This is a new story.
This has multiple lines.
All lines will be split.
This is the not last line.
This is a story.
'''

lines1 = story1.split("\n")
lines2 = story2.split("\n")
similarities = []

#print(lines1)
#print(lines2)

for line in lines1:
    if line in lines2:
        similarities.append(line)

print(similarities)



3 个答案:

答案 0 :(得分:1)

将您的stoy1和story2定义为avoid an empty line,例如:

story1 = '''This is a story.
This has multiple lines.
All lines will be split.
This is the last line.'''

或者您可以输入:

if line in lines2 and line != '':

代码:

story1 = '''This is a story.
This has multiple lines.
All lines will be split.
This is the last line.'''

story2 = '''This is a new story.
This has multiple lines.
All lines will be split.
This is the not last line.
This is a story.'''

lines1 = story1.split("\n")
lines2 = story2.split("\n")
similarities = []

for line in lines1:
    #if line in lines2 and line != '':
    if line in lines2:
        similarities.append(line)

print(similarities)

答案 1 :(得分:0)

第1行和第2行的输出:

In [2]: lines1
Out[2]:
['This is a story.',
 'This has multiple lines.',
 'All lines will be split.',
 'This is the last line.',
 '']

In [3]: lines2
Out[3]:
['This is a new story.',
 'This has multiple lines.',
 'All lines will be split.',
 'This is the not last line.',
 'This is a story.',
 '']
两个列表中的

都有一个空字符串,这是用多行块在“ \ n”上分割的结果。这就是为什么他们俩都把它当作“相似之处”

答案 2 :(得分:0)

你好,坎。

找到类似字符串后面的空字符串的原因是,两个故事中确实有一个空行。

story1 = '''This is a story.
This has multiple lines.
All lines will be split.
This is the last line.'''

story2 = '''This is a new story.
This has multiple lines.
All lines will be split.
This is the not last line.
This is a story.'''

由于末尾的'\ n'已被删除,因此上述内容不会添加空行。