首页 > 解决方案 > 如何检查句子特定部分之前的字符串是否与其他行中的任何文本匹配或不匹配(与特定部分之后相同)?

问题描述

我正在尝试检查第一个空格之前的文本是否与其他行匹配。如果它们匹配,我只想打印一次。同样,我想在第一个空格后检查文本。如果有任何重复,则只打印一次。

输入.txt

My school name: AVS school
Her school name: AVS school
My school name: ABC school
Their school name: XYZ school

输出.csv

My        school name: AVS school
          school name: ABC school
Their     school name: XYZ school

逻辑是它应该考虑第一个包括多少所学校。所以 My 由 2 个学校名称组成,并且重复了“My”这个词,所以我们只打印一次 My,学校名称是唯一的,所以我们必须打印 2 个名称。在第二行中,虽然起始词是唯一的学校名称已经重复,所以忽略。最后一行是唯一的,所以打印它。最后,我想先在栏目中的部分和在栏目中的学校名称。

试过了

with open ('input.txt', 'r') as f, open ('output.txt', 'w') as o:
    for line in f:
        a = f.split(' ', 1)
        if line in a:
            print (a[0])

任何帮助,将不胜感激。谢谢

标签: pythonfunctiontext

解决方案


import collections

s = """My school name: AVS school
Her school name: AVS school
My school name: ABC school
Their school name: XYZ school
My school name: DEF school"""

sentences = collections.defaultdict(list)

unique_suffixes = set()

for line in s.split('\n'):
    start, end = line.split(' ', maxsplit=1)
    if end not in unique_suffixes:
        unique_suffixes.add(end)
        sentences[start] += [end]

# Displaying the results
for start, ends in sentences.items():
    print(start, ends[0])
    for end in ends[1:]:
        print(len(start)*'-', end)

显示器

My school name: AVS school
-- school name: ABC school
-- school name: DEF school
Their school name: XYZ school

警告

这本字典没有排序,所以你可以先是“他们的”,然后是“我的”:

Their school name: XYZ school
My school name: AVS school
-- school name: ABC school
-- school name: DEF school

推荐阅读