我有一個.csv檔案,有幾列,其中一列是随機數,我想在那裡找到重複的值。如果有—奇怪的情況,但這畢竟是我要檢查的—我想顯示/存儲存儲存儲這些值的完整行。
要說清楚,我有這樣的東西:First, Whatever, 230, Whichever, etc
Second, Whatever, 11, Whichever, etc
Third, Whatever, 46, Whichever, etc
Fourth, Whatever, 18, Whichever, etc
Fifth, Whatever, 14, Whichever, etc
Sixth, Whatever, 48, Whichever, etc
Seventh, Whatever, 91, Whichever, etc
Eighth, Whatever, 18, Whichever, etc
Ninth, Whatever, 67, Whichever, etc
我想要:Fourth, Whatever, 18, Whichever, etc
Eighth, Whatever, 18, Whichever, etc
為了找到重複的值,我将該列存儲到字典中,并對每個鍵進行計數,以發現它們出現的次數。import csv
from collections import Counter, defaultdict, OrderedDict
with open(file, 'rt') as inputfile:
data = csv.reader(inputfile)
seen = defaultdict(set)
counts = Counter(row[col_2] for row in data)
print "Numbers and times they appear: %s" % counts
我明白了Counter({' 18 ': 2, ' 46 ': 1, ' 67 ': 1, ' 48 ': 1,...})
現在問題來了,因為我沒辦法把這個鍵和重複項聯系起來,然後再計算。如果我做了for value in counts:
if counts > 1:
print counts
我隻拿鑰匙,這不是我想要的,也不是所有的價值(更不用說我想列印的不僅僅是這個,還有整行…)
基本上我在找一種方法If there's a repeated number:
print rows containing those number
else
print "No repetitions"
提前謝謝。