Python删除重复名称
我有一个纯文本文件,每行都有文字:Python删除重复名称,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我有一个纯文本文件,每行都有文字: 3210 <DOCID>GH950102-000003<DOCID>/O 3243 Australia/LOCATION 3360 England/LOCATION 3414 India/LOCATION 3474 Melbourne/LOCATION 3497 England/LOCATION 3521 >India<TOPONYM>/O 3526 >Zimbab
3210 <DOCID>GH950102-000003<DOCID>/O
3243 Australia/LOCATION
3360 England/LOCATION
3414 India/LOCATION
3474 Melbourne/LOCATION
3497 England/LOCATION
3521 >India<TOPONYM>/O
3526 >Zimbabwe<TOPONYM>/O
3531 >England<TOPONYM>/O
3536 >Melbourne<TOPONYM>/O
3541 >England<TOPONYM>/O
3546 >England<TOPONYM>/O
3551 >Glasgow<TOPONYM>/O
3556 >England<TOPONYM>/O
3561 >England<TOPONYM>/O
3566 >Australia<TOPONYM>/O
3568 <DOCID>GH950102-000004<DOCID>/O
3739 Hampden/LOCATION
3821 Hampden/LOCATION
3838 Ibrox/LOCATION
3861 Neerday/LOCATION
4161 Fir Park/LOCATION
4229 Park<TOPONYM>/O
4234 >Hampden<TOPONYM>/O
4239 >Hampden<TOPONYM>/O
4244 >Midfield<TOPONYM>/O
4249 >Glasgow<TOPONYM>/O
4251 <DOCID>GH950102-000005<DOCID>/O
4535 Edinburgh/LOCATION
4840 Road<TOPONYM>/O
4845 >Edinburgh<TOPONYM>/O
4850 >Glasgow<TOPONYM>/O``
3210 GH950102-000003/O
3243澳大利亚/地点
3360英格兰/地点
3414印度/地点
3474墨尔本/位置
3497英格兰/地点
3521>印度/O
3526>津巴布韦/O
3531>英格兰/O
3536>墨尔本/O
3541>英格兰/O
3546>英格兰/O
3551>格拉斯哥/O
3556>英格兰/O
3561>英格兰/O
3566>澳大利亚/O
3568 GH950102-000004/O
3739汉普顿/地点
3821汉普顿/位置
3838 Ibrox/位置
3861 Neerday/地点
4161杉木公园/位置
4229公园/出口
4234>汉普顿/O
4239>汉普顿/O
4244>中场/O
4249>格拉斯哥/O
4251 GH950102-000005/O
4535爱丁堡/地点
北角4840号
4845>爱丁堡/O
4850>格拉斯哥/O``
我想删除此列表中的相同位置名称,它应该如下所示:
3210 <DOCID>GH950102-000003<DOCID>/O
3243 Australia/LOCATION
3360 England/LOCATION
3414 India/LOCATION
3474 Melbourne/LOCATION
3497 England/LOCATION
3526 >Zimbabwe<TOPONYM>/O
3551 >Glasgow<TOPONYM>/O
3568 <DOCID>GH950102-000004<DOCID>/O
3739 Hampden/LOCATION
3838 Ibrox/LOCATION
3861 Neerday/LOCATION
4161 Fir Park/LOCATION
4229 Park<TOPONYM>/O
4244 >Midfield<TOPONYM>/O
4249 >Glasgow<TOPONYM>/O
4251 <DOCID>GH950102-000005<DOCID>/O
4535 Edinburgh/LOCATION
4840 Road<TOPONYM>/O
4850 >Glasgow<TOPONYM>/O
3210 GH950102-000003/O
3243澳大利亚/地点
3360英格兰/地点
3414印度/地点
3474墨尔本/位置
3497英格兰/地点
3526>津巴布韦/O
3551>格拉斯哥/O
3568 GH950102-000004/O
3739汉普顿/地点
3838 Ibrox/位置
3861 Neerday/地点
4161杉木公园/位置
4229公园/出口
4244>中场/O
4249>格拉斯哥/O
4251 GH950102-000005/O
4535爱丁堡/地点
北角4840号
4850>格拉斯哥/O
我想删除重复的位置名称和docid应该保留在文件中。我知道有一种方法可以通过linux使用uniq,但如果我运行它,它将删除不同docid中的位置。
是否可以通过每个docid和docid内拆分它?如果位置名称相同,则应删除重复的名称。我是从手机上写的,因此这不是一个完整的解决方案,但关键点是:
import re
Docid=re.compile("^ *\d+ +<DOCID>")
Location=re.compile("^ *\d +>?(. +)/")
Lines={}
for line in file:
if re.match(Docid,line):
Lines={}
print line
else:
loc=re.findall(Location, line)[0]
if loc not in Lines.keys():
print line
Lines[loc] = True
重新导入
Docid=re.compile(“^*\d++”)
Location=re.compile(“^*\d++>?(.+)/”)
行={}
对于文件中的行:
如果重新匹配(文档ID,行):
行={}
打印行
其他:
loc=关于findall(位置、线)[0]
如果loc不在Lines.keys()中:
打印行
行[loc]=真
基本上,它会检查每一行代码,而不是一个新的docid。如果不是,则尝试读取位置,并查看是否已读取该位置。如果没有,则打印位置并将其添加到位置tead列表中
如果有新的docid,它将重置最后一个读取位置 这里有一个方法
import string
filename = 'testfile'
lines = tuple(open(filename, 'r'))
final_list = []
unique_list = [] # this resets itself every docid
for line in lines:
currentline = str(line)
if 'DOCID' in currentline:
unique_list = [] # this resets itself every docid
final_list.append(line)
else:
exclude = set(string.punctuation)
currentline = ''.join(ch if ch not in exclude else " " for ch in currentline)
city = currentline.split()[1]
if city not in unique_list:
unique_list.append(city)
final_list.append(line)
for line in final_list:
print(line)
输出:
3210 <DOCID>GH950102-000003<DOCID>/O
3243 Australia/LOCATION
3360 England/LOCATION
3414 India/LOCATION
3474 Melbourne/LOCATION
3526 >Zimbabwe<TOPONYM>/O
3551 >Glasgow<TOPONYM>/O
3568 <DOCID>GH950102-000004<DOCID>/O
3739 Hampden/LOCATION
3838 Ibrox/LOCATION
3861 Neerday/LOCATION
4161 Fir Park/LOCATION
4229 Park<TOPONYM>/O
4244 >Midfield<TOPONYM>/O
4249 >Glasgow<TOPONYM>/O
4251 <DOCID>GH950102-000005<DOCID>/O
4535 Edinburgh/LOCATION
4840 Road<TOPONYM>/O
4850 >Glasgow<TOPONYM>/O``
3210 GH950102-000003/O
3243澳大利亚/地点
3360英格兰/地点
3414印度/地点
3474墨尔本/位置
3526>津巴布韦/O
3551>格拉斯哥/O
3568 GH950102-000004/O
3739汉普顿/地点
3838 Ibrox/位置
3861 Neerday/地点
4161杉木公园/位置
4229公园/出口
4244>中场/O
4249>格拉斯哥/O
4251 GH950102-000005/O
4535爱丁堡/地点
北角4840号
4850>格拉斯哥/O``
注意:
testfile
是一个包含输入文本的文本文件。如果需要,您可以优化代码 您是否需要保留一个docid内位置的原始顺序?您是否关心保留哪个位置?docid内位置的原始顺序不是必需的。我只是希望一个docid中的位置不应该有重复,或者它删除带有标记的位置名称,或者带有标记/位置的位置名称。好的,我看到在您的预期结果中,您每个docid有两个英格兰。是吗?我很抱歉,但我想一个英格兰会为我工作。因为如果是一份文件中的两个英格兰,它将是重复的。左边的数字代表什么?如果存在重复,则这些数字不同。保留哪一份副本重要吗?