【发布时间】: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