Python 如何动态地初始化嵌套字典并为其赋值
我正在尝试动态启动Python中的嵌套字典并为其赋值,到目前为止我还没有成功,请帮助我,谢谢。 这就是我所尝试的Python 如何动态地初始化嵌套字典并为其赋值,python,Python,我正在尝试动态启动Python中的嵌套字典并为其赋值,到目前为止我还没有成功,请帮助我,谢谢。 这就是我所尝试的 thelist = [ ['2023', 'John Professa', 'Mfangano', '10 E', '84 A', '84 A', '3 E', '53 C', '65 B', '84 A', '84 A', '84 A', '84 A', '84 A', '400', 464, '66', '64', 'B', '1'], ['33332', 'd dd', 'Mf
thelist = [
['2023', 'John Professa', 'Mfangano', '10 E', '84 A', '84 A', '3 E', '53 C', '65 B', '84 A', '84 A', '84 A', '84 A', '84 A', '400', 464, '66', '64', 'B', '1'],
['33332', 'd dd', 'Mfangano', '40 D+', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '403', 40, '6', '4', 'E', '5'],
['3333', 'Isaac ', 'Mfangano', '83 A', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '402', 83, '12', '12', 'D-', '2'],
['1234', 'asdfas f', 'Mfangano', '60 B-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '390', 60, '9', '8', 'E', '4'],
['7777', 'asdfas f', 'Kujeni', '66 B', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '280', 66, '9', '9', 'E', '3'],
]
nested_Dictionary = {}
main_dorms = ['kujeni', 'Mfangano', 'Bakini', 'Kujeni', 'Flamingo']
for value in main_dorms:
nested_Dictionary[f'{value}'] = {}
nested_Dictionary[f'{value}']['A'] = [1]
nested_Dictionary[f'{value}']['A-'] = [1]
nested_Dictionary[f'{value}']['B+'] = [1]
nested_Dictionary[f'{value}']['B-'] = [1]
nested_Dictionary[f'{value}']['C+'] = [1]
nested_Dictionary[f'{value}']['C'] = [1]
nested_Dictionary[f'{value}']['C-'] = [1]
nested_Dictionary[f'{value}']['D+'] = [1]
nested_Dictionary[f'{value}']['D'] = [1]
nested_Dictionary[f'{value}']['D-'] = [1]
nested_Dictionary[f'{value}']['E'] = [1]
nested_Dictionary[f'{value}']['mean'] = [1]
nested_Dictionary[f'{value}']['meangrade'] = [1]
print(nested_Dictionary)
for value in thelist:
idnumber = value[0]
stream = value[2]
meangrade = value[18]
meanmarks = value[17]
print(idnumber, stream, meangrade, meanmarks)
nested_Dictionary[f'{stream}'][meangrade].append(meangrade)
nested_Dictionary[f'{stream}']['mean'].append(meanmarks)
nested_Dictionary[f'{stream}']['meangrade'].append(meanmarks)
print(nested_Dictionary)
这给了我一个关键错误
nested_Dictionary[f'{stream}'][meangrade].append(meangrade)
KeyError: 'Mfangano'
在meangrade周围添加引号,当我添加此代码时,您的代码工作正常
nested_Dictionary[f'{stream}']['meangrade'].append(meangrade)
添加到for循环中:
nested_Dictionary[f'{value}']['B'] = [1]
您应该使用一些可以让您动态构建锯齿形字典的东西来重新处理这个问题——它将更具可扩展性。我首选的实现方式是:
>>> from collections import defaultdict
>>>
>>> def defaulter():
... return defaultdict(defaulter)
...
>>> nested = defaultdict(defaulter)
>>> nested['a'][1]['c'][None] = True
>>> nested
defaultdict(<function defaulter at 0x7ffb8ba6db80>, {'a': defaultdict(<function defaulter at 0x7ffb8ba6db80>, {1: defaultdict(<function defaulter at 0x7ffb8ba6db80>, {'c': defaultdict(<function defaulter at 0x7ffb8ba6db80>, {None: True})})})})
FWIW,
f'{value}'
是非常多余的,只要value
就会做完全相同的事情……为什么要使用f'{value}'
作为键?为什么不nested\u Dictionary[value]
?我得到KeyError:'B'
。如果我运行这个,我得到KeyError:'B'
,这很有意义,因为'B'
从你的内键中丢失了。我尝试了你的代码,但没有得到你特定的KeyError
,而是得到了***KeyError:'B'
。无论采用哪种方法,问题都是在尝试使用特定键对字典进行索引之前,需要检查字典是否包含键。看看我不确定这是不是故意的行为meangrade
包含'B'
,它似乎是嵌套的_字典[f'{stream}']
的一个可能键。但是如果OP不澄清他想要代码做什么,我想我们无法知道
>>> nested['missing']
defaultdict(<function defaulter at 0x7ffb8ba6db80>, {})