【发布时间】:2020-11-20 01:10:08
【问题描述】:
假设 dict_lst = 字典列表。我正在尝试返回与 dict_lst 中以参数 k 作为键的最新 dict 关联的值。如果 k 不在 dict_lst 中的任何 dict 中,我们必须引发 KeyError。我该怎么做?
def get_value(dict_lst, k):
pass
示例:
dict_lst = [{'a':1, 'b':2, 'c':3}, {'c':4, 'd':5, 'e':6}, {'e' :7, 'f':8, 'g':9}]
d['c'] --> 4(发现第二个带有'c'的dict是最新的dict)
d['y'] --> KeyError
【问题讨论】:
标签: python loops class dictionary