gpt4 book ai didi

python 3 : nested dictionary with multiple keys from csv

转载 作者:太空宇宙 更新时间:2023-11-04 05:46:53 24 4
gpt4 key购买 nike

数据看起来像这样:

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))

关于 python 3 : nested dictionary with multiple keys from csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31843416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com