【问题标题】:Returning specific key(s) and its value from a dictionary从字典中返回特定键及其值
【发布时间】:2016-11-13 22:08:52
【问题描述】:

我正在编写一个函数,我需要分析字典并提取其中一个键及其所有值并将其返回。

字典示例:

{'P':[("Eight",1460, 225.0, 200.0, "fresco","Netherlands"),
      ("Six",1465,81.0, 127.1, "tempera", "Netherlands")],
 'V':[("Four",1661, 148.0, 257.0,"oil paint", "Austria"),
      ("Two",1630, 91.0, 77.0, "oil paint","USA")],
 'K':[("Five",1922,63.8,48.1,"watercolor","USA"),
      ("Seven",1950,61.0,61.0,"acrylic paint","USA"),
      ("Two",1965,81.3,100.3,"oil paint","United Kingdom")],
 'C':[("Ten",1496,365.0,389.0,"tempera","Italy")],
 'U':[("Nine",1203,182.0, 957.0,"egg tempera","Italy"),
      ("Twelve",1200,76.2,101.6,"egg tempera","France")]}

所以如果我的函数被称为 find_the_key 并且我正在寻找 'C' 及其值,那么它需要返回

find_the_key(dictionary2(),['C'])

return {'C': [('Ten', 1496, 365.0, 389.0, 'tempera','Italy')]}

如果它需要同时找到“C”和“V”,它会返回

find_the_key(dictionary2(),['C','V'])

return {'C': [('Ten', 1496, 365.0, 389.0, 'tempera', 'Italy')], 'V': [('Four',
1661, 148.0, 257.0, 'oil paint', 'Austria'), ('Two', 1630, 91.0, 77.0, 'oil
paint', 'USA')]}

我对从字典中提取键进行了一些研究,我当前的代码可能很接近。

def find_the_key(dictionary,thekey):
    for thekey in dictionary.keys():
      if dictionary[thekey] == targetkey:
        return key, targetkey

目前我收到错误 targetkey is not defined 虽然我认为我的逻辑是正确的。有谁知道如何做到这一点?

【问题讨论】:

  • 如果字典中不存在键,你想做什么?只是忽略它,引发错误,还是返回默认值?
  • @PM2Ring Jean-François Fabre 的解决方案是正确的,我认为只是忽略它
  • 我相信这两个答案都能在没有注意到的情况下解决问题:)

标签: python python-3.x dictionary find key


【解决方案1】:

两种方法:

1) 循环输入字典项每次扫描键列表中的键,可以,但不使用输入是字典的事实。如果键列表或输入字典很长,则可能需要一些时间(通过将键列表转换为 set 来稍微改进一下)

def find_the_key(dictionary,thekey):
    return {k:v for k,v in dictionary.items() if k in thekey}

2) 不在字典中执行循环,而是在询问的键中执行循环,如果键列表和字典很大,则更合乎逻辑且更快

def find_the_key(dictionary,thekey):
    return {k:dictionary[k] for k in thekey if k in dictionary}

小缺点:测试k是否属于,然后得到dictionary[k],这会哈希k两次

3) 经典,没有 listcomp,但只有 1 次使用 get 访问字典

def find_the_key(dictionary,thekey):
    d={}

    for k in thekey:
        v = dictionary.get(k)
        if k:
            d[k]=v
    return d

当然,所有方法都会产生相同的结果

【讨论】:

  • 在上一个版本中,您需要if v: 而不是if k:
  • 谢谢我给了你最好的答案,因为你所有的答案都很好,你还帮助我找出了另一个我遇到问题的功能
【解决方案2】:
def find_the_key(dictionary, target_keys):
    return {key: dictionary[key] for key in target_keys}

这使用了字典理解,这只是构建字典的一种便捷方式。您正在做的事情(在修复变量名之后)只会返回一对(元组)键和值。如果你从一个循环返回,你只会返回一件事:循环不会继续并同时返回所有其他类似的东西。

【讨论】:

  • 感谢您的快速响应,这实现了我的目标
【解决方案3】:

使用filter 函数的替代解决方案:
我故意在 filter_keys 中传递了不正确的密钥 J 来处理不同的情况

def find_the_key(dictionary, filter_keys):
    return dict(filter(lambda  i: i[0] in filter_keys, dictionary.items()))

print(find_the_key(dictionary, ['C','V','J']))

输出:

{'C': [('Ten', 1496, 365.0, 389.0, 'tempera', 'Italy')], 'V': [('Four', 1661, 148.0, 257.0, 'oil paint', 'Austria'), ('Two', 1630, 91.0, 77.0, 'oil paint', 'USA')]}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2018-02-10
    相关资源
    最近更新 更多