[英]Dividing dictionary into nested dictionaries, based on the key's name on Python 3.4
I have the following dictionary (short version, real data is much larger): 我有以下字典(简短版本,实际数据要大得多):
dict = {'C-STD-B&M-SUM:-1': 0, 'C-STD-B&M-SUM:-10': 4.520475, 'H-NSW-BAC-ART:-9': 0.33784000000000003, 'H-NSW-BAC-ART:0': 0, 'H-NSW-BAC-ENG:-59': 0.020309999999999998, 'H-NSW-BAC-ENG:-6': 0,}
I want to divide it into smaller nested dictionaries, depending on a part of the key name. 我想根据键名的一部分将其分成较小的嵌套字典。
Expected output would be: 预期输出为:
# fixed closing brackets
dict1 = {'C-STD-B&M-SUM: {'-1': 0, '-10': 4.520475}}
dict2 = {'H-NSW-BAC-ART: {'-9': 0.33784000000000003, '0': 0}}
dict3 = {'H-NSW-BAC-ENG: {'-59': 0.020309999999999998, '-6': 0}}
Logic behind is: 背后的逻辑是:
dict1: if the part of the key name is 'C-STD-B&M-SUM', add to dict1.
dict2: if the part of the key name is 'H-NSW-BAC-ART', add to dict2.
dict3: if the part of the key name is 'H-NSW-BAC-ENG', add to dict3.
Partial code so far: 到目前为止的部分代码:
def divide_dictionaries(dict):
c_std_bem_sum = {}
for k, v in dict.items():
if k[0:13] == 'C-STD-B&M-SUM':
c_std_bem_sum = k[14:17], v
What I'm trying to do is to create the nested dictionaries that I need and then I'll create the dictionary and add the nested one to it, but I'm not sure if it's a good way to do it. 我想做的是创建所需的嵌套字典,然后创建字典并向其中添加嵌套字典,但是我不确定这是否是一个好方法。
When I run the code above, the variable c_std_bem_sum becomes a tuple, with only two values that are changed at each iteration. 当我运行上面的代码时,变量c_std_bem_sum成为一个元组,每次迭代仅更改两个值。 How can I make it be a dictionary, so I can later create another dictionary, and use this one as the value for one of the keys? 如何使它成为字典,以便以后可以创建另一本字典,并将其用作其中一个键的值?
One way to approach it would be to do something like 一种解决方法是做类似的事情
d = {'C-STD-B&M-SUM:-1': 0, 'C-STD-B&M-SUM:-10': 4.520475, 'H-NSW-BAC-ART:-9': 0.33784000000000003, 'H-NSW-BAC-ART:0': 0, 'H-NSW-BAC-ENG:-59': 0.020309999999999998, 'H-NSW-BAC-ENG:-6': 0,}
def divide_dictionaries(somedict):
out = {}
for k,v in somedict.items():
head, tail = k.split(":")
subdict = out.setdefault(head, {})
subdict[tail] = v
return out
which gives 这使
>>> dnew = divide_dictionaries(d)
>>> import pprint
>>> pprint.pprint(dnew)
{'C-STD-B&M-SUM': {'-1': 0, '-10': 4.520475},
'H-NSW-BAC-ART': {'-9': 0.33784000000000003, '0': 0},
'H-NSW-BAC-ENG': {'-59': 0.020309999999999998, '-6': 0}}
A few notes: 一些注意事项:
(1) We're using nested dictionaries instead of creating separate named dictionaries, which aren't convenient. (1)我们使用嵌套字典,而不是创建单独的命名字典,这并不方便。
(2) We used setdefault , which is a handy way to say "give me the value in the dictionary, but if there isn't one, add this to the dictionary and return it instead.". (2)我们使用setdefault ,这是一种方便的说法:“给我字典中的值,但是如果没有,请将其添加到字典中并返回它。” Saves an if
. 保存一个if
。
(3) We can use .split(":")
instead of hardcoding the width, which isn't very robust -- at least assuming that's the delimiter, anyway! (3)我们可以使用.split(":")
来代替对宽度进行硬编码,因为它不是很健壮-至少假设这是分隔符!
(4) It's a bad idea to use dict
, the name of a builtin type, as a variable name. (4)使用内建类型的名称dict
作为变量名是一个坏主意。
That's because you're setting your dictionary and overriding it with a tuple: 那是因为您要设置字典并用元组覆盖它:
>>> a = 1, 2
>>> print a
>>> (1,2)
Now for your example: 现在为您的示例:
>>> def divide_dictionaries(dict):
>>> c_std_bem_sum = {}
>>> for k, v in dict.items():
>>> if k[0:13] == 'C-STD-B&M-SUM':
>>> new_key = k[14:17] # sure you don't want [14:], open ended?
>>> c_std_bem_sum[new_key] = v
Basically, this grabs the rest of the key (or 3 characters, as you have it, the [14:None] or [14:] would get the rest of the string) and then uses that as the new key for the dict. 基本上,这将获取其余的键(或者,如果有3个字符,则[14:None]或[14:]将获得字符串的其余部分),然后将其用作dict的新键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.