如果條目的順序都沒有,我建議使用字典來跟蹤復發的重要:
sort_src = list("hello world") #for testing
seen = {}
for row1 in sort_src:
seen[row1] = seen.get(row1,0) + 1
#if the row is already in the dict then it's value is increased by 1
#if the row is not in the dict then .get() returns 0 (then add 1
for r in seen:
print(r,"occured",seen[r],"times")
如果行的順序也沒關係一樣可以用collections.OrderedDict()
import collections
seen = collections.OrderedDict()
應用兩種方式seen.keys()
會給你唯一條目和的列表將是一個(entry,count)元組列表。
編輯 - 僅計算你需要總結的總條目副本的數目(dict.values()),然後減去獨特的條目數(字典的LEN)
num_of_dup_entries = sum(seen.values()) - len(seen)
您應該使用字典而不是兩個列表。 – SirParselot
如果您不介意使用其他庫,'pandas'可以輕鬆讀取csv文件並具有'DataFrame.duplicated'功能,可以非常輕鬆地識別重複項。 – Tgsmith61591
什麼是關鍵?整行或每行內的一組列?你之後有什麼 - 計算每個重複的行或所有的dups的數量? – mhawke