如何在嵌套字典中追加值
How to append values in nested dictionary
dictnry={token1:{1:freq1,2:freq1a},
,token2:{1:freq2,2:freq2a,3:freq2b}
,token3:{3:freq4}}
我如何循环遍历字典 int:dict 作为 key:value 对和
附加一个新词典(例如:{5:freq5}
in token1
and {1:freq11}
in token3
) 进入它的第二个现有字典
for loop in dictnry:
condition==true:
for loop in sec_dictnry:
p={5:freq5} #p IS THE NEW DICTIONARY & 5 and freq5 will be substituted with variables
dictnry[token1].append_for_dictionary_function_or_something(p) #WHAT DO I DO IN THIS LINE
dictnry={token1:{1:freq1,2:freq1a,5:freq5},
,token2:{1:freq2,2:freq2a,3:freq2b}
,token3:{3:freq4,1:freq11}}
你可以简单地遍历你的字典,一旦键等于你想在它的值中附加一些字典的键,然后像下面那样做
例子
from pprint import pprint
dictionary = {
'token1':{1:'freq1',2:'freq1a'},
'token2':{1:'freq2',2:'freq2a',3:'freq2b'},
'token3':{3:'freq4'}
}
for x, y in dictionary.items():
if x == 'token1':
y[5] = 'freq5'
if x == 'token3':
y[1] = 'freq11'
pprint(dictionary)
或
dictionary['token1'][5] = 'freq5'
dictionary['token3'][1] = 'freq11'
输出
{'token1': {1: 'freq1', 2: 'freq1a', 5: 'freq5'},
'token2': {1: 'freq2', 2: 'freq2a', 3: 'freq2b'},
'token3': {1: 'freq11', 3: 'freq4'}}
您可以为要添加的内容创建另一个字典:
import json
d = {
"token1": {
1: "freq1",
2: "freq1a",
},
"token2": {
1: "freq2",
2: "freq2a",
3: "freq2b",
},
"token3": {
3: "freq4",
}
}
print("Before:")
print(json.dumps(d, indent=4))
key_entry_to_add = {
"token1": {
5: "freq5",
},
"token3": {
1: "freq11",
}
}
for key, entry in key_entry_to_add.items():
entry_key, entry_val = next(iter(entry.items()))
d[key][entry_key] = entry_val
print("After:")
print(json.dumps(d, indent=4, sort_keys=True))
输出:
Before:
{
"token1": {
"1": "freq1",
"2": "freq1a"
},
"token2": {
"1": "freq2",
"2": "freq2a",
"3": "freq2b"
},
"token3": {
"3": "freq4"
}
}
After:
{
"token1": {
"1": "freq1",
"2": "freq1a",
"5": "freq5"
},
"token2": {
"1": "freq2",
"2": "freq2a",
"3": "freq2b"
},
"token3": {
"1": "freq11",
"3": "freq4"
}
}
dictnry={token1:{1:freq1,2:freq1a},
,token2:{1:freq2,2:freq2a,3:freq2b}
,token3:{3:freq4}}
我如何循环遍历字典 int:dict 作为 key:value 对和
附加一个新词典(例如:{5:freq5}
in token1
and {1:freq11}
in token3
) 进入它的第二个现有字典
for loop in dictnry:
condition==true:
for loop in sec_dictnry:
p={5:freq5} #p IS THE NEW DICTIONARY & 5 and freq5 will be substituted with variables
dictnry[token1].append_for_dictionary_function_or_something(p) #WHAT DO I DO IN THIS LINE
dictnry={token1:{1:freq1,2:freq1a,5:freq5},
,token2:{1:freq2,2:freq2a,3:freq2b}
,token3:{3:freq4,1:freq11}}
你可以简单地遍历你的字典,一旦键等于你想在它的值中附加一些字典的键,然后像下面那样做
例子
from pprint import pprint
dictionary = {
'token1':{1:'freq1',2:'freq1a'},
'token2':{1:'freq2',2:'freq2a',3:'freq2b'},
'token3':{3:'freq4'}
}
for x, y in dictionary.items():
if x == 'token1':
y[5] = 'freq5'
if x == 'token3':
y[1] = 'freq11'
pprint(dictionary)
或
dictionary['token1'][5] = 'freq5'
dictionary['token3'][1] = 'freq11'
输出
{'token1': {1: 'freq1', 2: 'freq1a', 5: 'freq5'},
'token2': {1: 'freq2', 2: 'freq2a', 3: 'freq2b'},
'token3': {1: 'freq11', 3: 'freq4'}}
您可以为要添加的内容创建另一个字典:
import json
d = {
"token1": {
1: "freq1",
2: "freq1a",
},
"token2": {
1: "freq2",
2: "freq2a",
3: "freq2b",
},
"token3": {
3: "freq4",
}
}
print("Before:")
print(json.dumps(d, indent=4))
key_entry_to_add = {
"token1": {
5: "freq5",
},
"token3": {
1: "freq11",
}
}
for key, entry in key_entry_to_add.items():
entry_key, entry_val = next(iter(entry.items()))
d[key][entry_key] = entry_val
print("After:")
print(json.dumps(d, indent=4, sort_keys=True))
输出:
Before:
{
"token1": {
"1": "freq1",
"2": "freq1a"
},
"token2": {
"1": "freq2",
"2": "freq2a",
"3": "freq2b"
},
"token3": {
"3": "freq4"
}
}
After:
{
"token1": {
"1": "freq1",
"2": "freq1a",
"5": "freq5"
},
"token2": {
"1": "freq2",
"2": "freq2a",
"3": "freq2b"
},
"token3": {
"1": "freq11",
"3": "freq4"
}
}