python - 如何检查句子特定部分之前的字符串是否与其他行中的任何文本匹配或不匹配(与特定部分之后相同)?
问题描述
我正在尝试检查第一个空格之前的文本是否与其他行匹配。如果它们匹配,我只想打印一次。同样,我想在第一个空格后检查文本。如果有任何重复,则只打印一次。
输入.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])
任何帮助,将不胜感激。谢谢
解决方案
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
推荐阅读
- ios - 带有大标题的 UISearchController,UI 在关闭时行为不端
- java - 将 JaVers 从 3.14 迁移到 5.9.1
- actions-on-google - Google 智能助理 - 产品发布后“获取测试版本”
- javascript - 使用 selenium 和 python 提交 reCAPTCHA v2 - 没有表单提交按钮
- kotlin - 带有 Quarkus 的 MockServer:“发现了多个竞争的 ParameterResolver”
- sql - Postgresql:加入具有不同时间尺度的 TimeSeries 数据:每周、每日和每月
- avr - 无法使用 atmega2560 传输字符串
- python-3.x - Python从txt.file创建Person类和加载函数
- vue.js - Vue.js 路由参数
- python - 可以在 Python 中获取我的 if 条件中应用的条件吗?