【问题标题】:How to append values in nested dictionary如何在嵌套字典中附加值
【发布时间】:2021-02-22 11:03:51
【问题描述】:
dictnry={token1:{1:freq1,2:freq1a},
        ,token2:{1:freq2,2:freq2a,3:freq2b}
        ,token3:{3:freq4}}

我如何循环使用 int:dict 作为键:值对的字典和 追加一个新字典(例如:{5:freq5} in token1 和 {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}}

【问题讨论】:

  • 我不确定您的问题是什么;假设你有outer_dict={'inner_dict1':{'value1':"x1"}},如果你想追加新的字典,你可以这样做:outer_dict["inner_dict2"] = {...},如果你想更新内部字典中的值,那么你可以这样做outer_dict["inner_dict1"]["value1"] = "new_value";要为内部 dict 创建新密钥,您正在做同样的事情 outer_dict["inner_dict1"]["value2"] = "new_value"
  • 看起来您已经编写了一些伪代码,但还没有尝试实现它。你的问题 (how do I do this) 太宽泛了:不清楚你对伪代码的哪一部分有问题。欢迎来到 SO。这不是讨论论坛或教程。请使用tour 并花时间阅读How to Ask 以及该页面上的其他链接。花一些时间与the Tutorial 一起练习示例。它会让你了解 Python 提供的工具来帮助你解决问题。

标签: python dictionary append


【解决方案1】:

你可以简单地遍历你的字典,一旦键等于你想在它的值中附加一些字典的键,然后像下面那样做

示例

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'}}

【讨论】:

    【解决方案2】:

    您可以为要添加的内容创建另一个字典:

    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"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 2020-08-07
      • 2020-06-01
      • 2021-08-26
      相关资源
      最近更新 更多