簡體   English   中英

如何檢查句子特定部分之前的字符串是否與其他行中的任何文本匹配或不匹配(與特定部分之后相同)?

[英]how to check whether the string before specific part of a sentence matches with any of the text in other lines or no (same with after specific part)?

我正在嘗試檢查第一個空格之前的文本是否與其他行匹配。 如果它們匹配,我只想打印一次。 同樣,我想在第一個空格后檢查文本。 如果有任何重復,則只打印一次。

輸入.txt

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

output.csv

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

邏輯是首先應該考慮有多少學校。 所以 My 由 2 個學校名稱組成,並且重復了“My”這個詞,所以我們只打印一次 My 並且學校名稱是唯一的,所以我們必須打印 2 個名稱。 在第二行中,盡管起始單詞是唯一的學校名稱已經重復,因此請忽略。 最后一行是 uniques 所以打印它。 最后,我想在第一個欄目和學校名稱欄目中。

嘗試過

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])

任何幫助,將不勝感激。 謝謝

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM