【问题标题】:Get key from a list of values in dictionary?从字典中的值列表中获取键?
【发布时间】:2022-06-21 01:06:07
【问题描述】:

假设我有:

ref = ['<var>', '<id>', '<expr>']
val = [['a', 'b', 'c'], 'a', '1+1']
dicio = dict(zip(ref, val))

现在我知道了

list(dicio.keys())[list(dicio.values()).index('a')]

它返回&lt;id&gt;。但是假设每个键只有一个关联值,所以

val = [['a', 'b', 'c'], 'b', '1+1']

我怎样才能得到&lt;var&gt;? 谢谢。

【问题讨论】:

标签: python


【解决方案1】:

只需将"a" 值替换为list ['a', 'b', 'c']

print(list(dicio.keys())[list(dicio.values()).index(['a', 'b', 'c'])])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多