python删除id相同的行
PHP中文网
PHP中文网 2017-04-17 17:34:13
0
2
321

有个文件的结构如下
The Cunctator 334555717

64.38.175.xxx 334555718

The Cunctator 334555718

The Cunctator 334555720

The Cunctator 334555721

AxelBoldt 334555723

The Cunctator 334555723

对于相同的id而言,我只需要保存最后一个id的数据,请问该怎么用python处理。比如上面的结果应该为:
The Cunctator 334555717

The Cunctator 334555718

The Cunctator 334555720

The Cunctator 334555721

The Cunctator 334555723

PHP中文网
PHP中文网

认证0级讲师

membalas semua(2)
左手右手慢动作

Gelung, id digunakan sebagai kunci kamus, lintasi

伊谢尔伦
with open('input', 'r') as f:
    lines = reversed(filter(bool, f.readlines()))

s = set()
result = []
for line in lines:
    id = line.split()[-1]
    if id in s: continue
    result.append(line)
    s.add(id)
result.reverse()

Pengekodan mudah alih belum diuji

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan