在默认字典中嵌套默认字典
Nesting a default dictionary within a default dictionary
我正在尝试读取这样格式的 csv
Number,Letter,Color,
1,a,blue,
1,b,green,
1,c,yellow,
并得到一个像这样的嵌套字典
{
"1":[
"Letter":["a","b","c"],
"Color":["blue","green","yellow"]
]
}
我可以从 csv 中获取数字及其关联的行,但是当我尝试嵌套时,我得到了 AttributeError: 'collections.defaultdict' object has no attribute 'append',我想我大部分时间都关闭了向右开始
result = co.defaultdict(lambda: co.defaultdict(list))
subresult = co.defaultdict(list)
with open(os.path.join(inputdir,tablelist[i])) as f:
csv_reader = csv.reader(f)
csv_headings = next(csv_reader)
read = csv.DictReader(f, fieldnames=csv_headings)
for line in read:
subresult = {}
for j in range(1,len(csv_headings)):
#result[line[csv_headings[0]]].append(line[csv_headings[j]])
result[line[csv_headings[0]]].append(subresult[csv_headings[j]].append(line[csv_headings[j]]))
dict对象没有append方法而是使用update方法
类似于:
result[line[csv_headings[0]]].update(subresult[csv_headings[j]].append(line[csv_headings[j]]))
例如:
In [1]: a ={'a':1}
In [2]: a.append({'b':2})
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-02c9b6cd9396> in <module>()
----> 1 a.append({'b':2})
AttributeError: 'dict' object has no attribute 'append'
In [3]: a.update({'b':2})
In [3]: a
Out[3]: {'a': 1, 'b': 2}
删除文件中的 header 行,这有效:
from collections import defaultdict
d = defaultdict(lambda : defaultdict(list))
with open('myFile.txt', 'r') as f:
for line in f:
r = line.strip().split(",")
number, letter, color = r[:3]
d[number]['Letter'].append(letter)
d[number]['Color'].append(color)
print d
输出
defaultdict(<function <lambda> at 0x7f3d99f49b90>, {'1': defaultdict(<type 'list'>,
{'Color': ['blue', 'green', 'yellow'], 'Letter': ['a', 'b', 'c']})})
我正在尝试读取这样格式的 csv
Number,Letter,Color,
1,a,blue,
1,b,green,
1,c,yellow,
并得到一个像这样的嵌套字典
{
"1":[
"Letter":["a","b","c"],
"Color":["blue","green","yellow"]
]
}
我可以从 csv 中获取数字及其关联的行,但是当我尝试嵌套时,我得到了 AttributeError: 'collections.defaultdict' object has no attribute 'append',我想我大部分时间都关闭了向右开始
result = co.defaultdict(lambda: co.defaultdict(list))
subresult = co.defaultdict(list)
with open(os.path.join(inputdir,tablelist[i])) as f:
csv_reader = csv.reader(f)
csv_headings = next(csv_reader)
read = csv.DictReader(f, fieldnames=csv_headings)
for line in read:
subresult = {}
for j in range(1,len(csv_headings)):
#result[line[csv_headings[0]]].append(line[csv_headings[j]])
result[line[csv_headings[0]]].append(subresult[csv_headings[j]].append(line[csv_headings[j]]))
dict对象没有append方法而是使用update方法
类似于:
result[line[csv_headings[0]]].update(subresult[csv_headings[j]].append(line[csv_headings[j]]))
例如:
In [1]: a ={'a':1}
In [2]: a.append({'b':2})
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-02c9b6cd9396> in <module>()
----> 1 a.append({'b':2})
AttributeError: 'dict' object has no attribute 'append'
In [3]: a.update({'b':2})
In [3]: a
Out[3]: {'a': 1, 'b': 2}
删除文件中的 header 行,这有效:
from collections import defaultdict
d = defaultdict(lambda : defaultdict(list))
with open('myFile.txt', 'r') as f:
for line in f:
r = line.strip().split(",")
number, letter, color = r[:3]
d[number]['Letter'].append(letter)
d[number]['Color'].append(color)
print d
输出
defaultdict(<function <lambda> at 0x7f3d99f49b90>, {'1': defaultdict(<type 'list'>,
{'Color': ['blue', 'green', 'yellow'], 'Letter': ['a', 'b', 'c']})})