我正在尝试制作一个非常简单的计数脚本,我猜是使用了defaultdict (我无法理解如何使用DefaultDict,因此如果有人可以评论我的代码片段,我将非常感激)
我的目标是取元素0和元素1,将它们合并成一个字符串,然后计算有多少唯一的字符串.
例如,在下面的数据中,有15行包含3个类,4个分类,当合并在一起时,我们只有3个唯一的类。第一行的合并数据(忽略标题行)是:Class01CD2
CSV数据:
uniq1,uniq2,three,four,five,six
Class01,CD2,data,data,data,data
Class01,CD2,data,data,data,data
Class01,CD2,data,data,data,data
Class01,CD2,data,data,data,data
Class02,CD3,data,data,data,data
Class02,CD3,data,data,data,data
Class02,CD3,data,data,data,data
Class02,CD3,data,data,data,data
Class02,CD3,data,data,data,data
Class02,CD3,data,data,data,data
Class02,CD3,data,data,data,data
DClass2,DE2,data,data,data,data
DClass2,DE2,data,data,data,data
Class02,CD1,data,data,data,data
Class02,CD1,data,data,data,data它的想法是简单地打印出有多少唯一的类可用。有人能帮我解决这个问题吗?
问候
发布于 2013-11-10 10:11:29
由于您正在处理CSV数据,所以可以使用CSV模块以及字典::。
import csv
uniq = {} #Create an empty dictionary, which we will use as a hashmap as Python dictionaries support key-value pairs.
ifile = open('data.csv', 'r') #whatever your CSV file is named.
reader = csv.reader(ifile)
for row in reader:
joined = row[0] + row[1] #The joined string is simply the first and second columns in each row.
#Check to see that the key exists, if it does increment the occurrence by 1
if joined in uniq.keys():
uniq[joined] += 1
else:
uniq[joined] = 1 #This means the key doesn't exist, so add the key to the dictionary with an occurrence of 1
print uniq #Now output the results这一产出如下:
{'Class02CD3': 7, 'Class02CD1': 2, 'Class01CD2': 3, 'DClass2DE2': 2}注意:这是假设CSV没有标题行(uniq1,uniq2,three,four,five,six)。
参考资料:
http://docs.python.org/2/library/stdtypes.html#dict
https://stackoverflow.com/questions/19888255
复制相似问题