简体   繁体   English

dict.setdefault将一个额外的(默认值)项添加到值列表中

[英]dict.setdefault appends one extra (default?) item into the value list

I am using a dictionary to group data from a CSV file, so for instance fist and second columns are the dict key and value will be a list of tuples with column 3,4. 我正在使用字典对CSV文件中的数据进行分组,因此,例如,拳头和第二列是dict键,值将是具有3,4列的元组列表。

my code snippet is: 我的代码段是:

import csv
import collections
csvDicData_ = dict()

fh = open('myfile.csv', 'rt')
reader = csv.reader(fh, delimiter=';', skipinitialspace=True)
for indx, row in enumerate(reader):
    if row:
        #-- put in a dictionary form: #csvDicData_[(row[0],row[1])] = (row[2],row[3])
        key   = (row[0],row[1])
        value = (row[2],row[3])
        #-- I'd like to use the row below (commented) insted of the next two, I expect the same result... 
        #csvDicData_.setdefault(key,[value]).append(value)

        if (not key in csvDicData_): csvDicData_[key] = [value]
        else: csvDicData_[key].append(value)

The code above produces the correct result, altough I tried to use csvDicData_.setdefault(key,[value]).append(value) and for some reason that I do not understand, the len(csvDicData_[('field1x','field2x')] )) has always one more item (with value (0,0) that expected. 上面的代码产生了正确的结果,尽管我尝试使用csvDicData_.setdefault(key,[value]).append(value) ,但由于某些我不理解的原因, len(csvDicData_[('field1x','field2x')] ))总是多一个预期的项目(值(0,0))。

Why this behaviour (it is like the first line in the CSV file for each key automatically adds the tuple (0,0) to the dictionary/key. 为什么会出现这种行为(就像每个键的CSV文件中的第一行一样)会自动将元组(0,0)添加到字典/键中。

When you do 当你做

csvDicData_.setdefault(key,[value]).append(value)

you initialize the list as [value] , if missing, and then append value to it, giving you [value, value] . 您可以将列表初始化为[value] (如果缺少),然后将value附加到列表中,从而得到[value, value] What you want instead is to initialize with an empty list: 相反,您要使用一个空列表进行初始化:

csvDicData_.setdefault(key,[]).append(value)

or use collections.defaultdict(list) and just do 或使用collections.defaultdict(list)并执行

csvDicData_[key].append(value)

The very first time when you do 第一次做

csvDicData_.setdefault(key,[value]).append(value)

the key will not be there in the dictionary and so the key will be created with the value as [value] . key将不在字典中,因此将使用[value]值创建键。 Now, setdefault returns the value corresponding to the key . 现在, setdefault返回与key对应的值。 So, [value] is returned and you are appending value to it. 因此,将返回[value] ,并且您要向其value That is why you are always getting one extra element. 这就是为什么您总是得到一个额外的元素。

This should have been just 这本来应该

csvDicData_.setdefault(key, []).append(value)

now, the empty list will be returned and you will be appending value to it. 现在,将返回空列表,您将为其添加value


Apart from that, you might want to open the file with with statement, like this 除此之外,您可能希望使用with语句打开文件,如下所示

with open('myfile.csv', 'rt') as fh:
    reader = csv.reader(fh, delimiter=';', skipinitialspace=True)
    for indx, row in enumerate(reader):
        ....

so that you don't have to worry about explicitly closing the file. 这样您就不必担心显式关闭文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM