Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过合并两列数据来计算有多少唯一标识符?_Python_Python 2.7_Csv_Defaultdict - Fatal编程技术网

Python 通过合并两列数据来计算有多少唯一标识符?

Python 通过合并两列数据来计算有多少唯一标识符?,python,python-2.7,csv,defaultdict,Python,Python 2.7,Csv,Defaultdict,我正在尝试使用defaultdict制作一个非常简单的计数脚本(我不知道如何使用defaultdict,所以如果有人能给我评论一些代码,我将不胜感激) 我的目标是获取元素0和元素1,将它们合并为一个字符串,然后计算有多少个唯一的字符串 例如,在下面的数据中,有15行包含3个类,4个ClassID,当合并在一起时,我们只有3个唯一的类。第一行的合并数据(忽略标题行)是:Class01CD2 CSV数据: 它的想法是简单地打印出有多少唯一的类可用。 有人能帮我解决这个问题吗 问候 -Hyflex由于

我正在尝试使用defaultdict制作一个非常简单的计数脚本(我不知道如何使用defaultdict,所以如果有人能给我评论一些代码,我将不胜感激)

我的目标是获取元素0和元素1,将它们合并为一个字符串,然后计算有多少个唯一的字符串

例如,在下面的数据中,有15行包含3个类,4个ClassID,当合并在一起时,我们只有3个唯一的类。第一行的合并数据(忽略标题行)是:
Class01CD2

CSV数据: 它的想法是简单地打印出有多少唯一的类可用。 有人能帮我解决这个问题吗

问候

-Hyflex

由于您正在处理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、三、四、五、六

参考资料:


由于您正在处理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、三、四、五、六

参考资料:


@Hyflex:感谢您的编辑。直到你提出建议,我才发现我打错了。虽然你编写的代码肯定会派上用场,但这并不是我想要实现的。输出应该是一个简单的3(因为它们是3个唯一的类。但是,在使用简单的:
print len(uniq)编码之后,这很容易做到)
@PatrickKostjens将其从ifile更改为file有什么原因吗?@Hyflex,对不起,我的错,我在查看编辑时误读了消息。@PatrickKostjens:注意:
file
是内置Python函数的名称。@Hyflex:谢谢您的编辑。在您提出之前,我没有发现键入错误。而您编写的代码将定义它ely会派上用场的,这不是我想要实现的。输出应该只是一个简单的3(因为它们是3个唯一的类。但是,在使用简单的:
print len(uniq)编码之后,这很容易做到
@PatrickKostjens将其从ifile更改为file有什么原因吗?@Hyflex,很抱歉,我在查看编辑时误读了消息。@PatrickKostjens:Note:
file
是内置Python函数的名称。