Skip to main content
 首页 » 编程设计

python 3 : nested dictionary with multiple keys from csv

2025年05月04日12tuyile006

数据看起来像这样:

id,outer,inner1,inner2,inner3 
123,"Smith,John",a,b,c 
123,"Smith,John",d,e,f 
123,"Smith,John",g,h,i 
456,"Williams,Tim",xx,yy,zz 
456,"Williams,Tim",vv,ww,uu 
456,"Miller,Ray",rrr,sss,ttt 
456,"Miller,Ray",qqq,www,ppp 

我希望生成的字典是

{'123': {'Smith,John': 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}, 
 '456': {'Williams,Tim': 'xx', 'yy', 'zz', 'vv', 'ww', 'zz'}, 
        {'Miller,Ray': 'rrr', 'sss', 'ttt', 'qqq', 'www', 'ppp'}} 

我尝试改编 Python Creating A Nested Dictionary From CSV File 接受的答案,但此方法会在每一行覆盖字典,因此只有每个 id 的最后一行最终出现在字典中。

请您参考如下方法:

collections.defaultdict 使用每行的第一个元素作为外部字典键,然后使用第二个元素作为内部字典键,并将行中的其余值添加到列表中作为内部字典键的值:

import csv 
from collections import defaultdict 
with open("in.txt" ) as f: 
    next(f) # skip header 
    d = defaultdict(lambda: defaultdict(list)) 
    r = csv.reader(f) 
    for row in r: 
        d[row[0]][row[1]].extend(row[2:]) 
 
from pprint import pprint as pp 
 
pp(dict(d)) 

输出:

{'123': {'Smith,John': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']}, 
 '456': {'Miller,Ray': ['rrr', 'sss', 'ttt', 'qqq', 'www', 'ppp'], 
         'Williams,Tim': ['xx', 'yy', 'zz', 'vv', 'ww', 'uu']}} 

由于您使用的是 python3,我们可以使用 * 在循环中解压缩以使代码更好一些:

with open("in.txt") as f: 
    next(f)  # skip header 
    d = defaultdict(lambda: defaultdict(list)) 
    r = csv.reader(f) 
    for k1, k2, *vals in r: 
        d[k1][k2].extend(vals))