我有一个.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"
提前谢谢。