- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
从列表中读取键后,我需要更新字典中的值。例如
d1 = {"sun":{"mon":"111"},"tue":{"wed":"222"},"thu":{"fri":{"sat":"333"}}} and l1 = ["thu","fri","sat"].
这里我需要将d1["thu"]["fri"]["sat"]
的值更改为444
。
请告诉我如何编写一个以 d1、l1 和新值作为参数并返回更新后的字典的函数。
最佳答案
或者您可以创建一个新字典并用它来更新原始字典
def create_update_dict(keys, val):
if len(keys) == 1:
return {keys[0]: val}
else:
return {keys[0]: create_update_dict(keys[1:], val)}
def update_dict_val(d, val, keys):
update_dict = create_update_dict(keys, val)
d.update(update_dict)
return d
关于 python : update value in nested dictonary after reading keys from list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41007437/
var x = Object.create(null); x["hello"] = "world"; 但是我可以允许未经验证的用户输入作为键吗?我想将它用作玩家名称 -> 玩家对象 map 。玩家名称
我想获取以下词典的 json 字符串,但我不知道正确的方法是什么。 /* For example, I have a Dictionary contains a set of users and I
在我的一个应用程序中,我必须使用许多以自定义对象作为键的字典。为了提高查找的性能,我实现了一个覆盖 GetHashCode 的基类。它似乎可以工作,但不知何故我仍然对此有一种不好的感觉,所以我决定发布
从列表中读取键后,我需要更新字典中的值。例如 d1 = {"sun":{"mon":"111"},"tue":{"wed":"222"},"thu":{"fri":{"sat":"333"}}} an
我是一名优秀的程序员,十分优秀!