我有以下函數構建用於對行進行排序,然後每行將行中的內容排序爲數字值。Python程序刪除行中的重複條目
線路是這樣的:
67:1 45:1 67:1 89:1
31:1 89:5 45:1 23:1
代碼:
with open("SVM/svm-pos-train.txt") as f, open("SVM/svm-pos-train2.txt", 'w') as out:
for line in f:
line = line.split()
line.sort(key = lambda x: int(x.split(':')[0]))
out.write(" ".join(line) + '\n')
如何修改這個讓也從當前行刪除重複的,就像這樣:
45:1 67:1 89:1
23:1 31:1 45:1 89:1
我一直在搜索和試用/(很多)錯誤,但沒有運氣。
應67:1和67:2被認爲是一樣的嗎? – oleg
@oleg是的,我應該想到 –
在這種情況下'''set(line.split())'''將不起作用 – oleg