获取键对应于字典中的值

时间:2011-12-27 23:42:35

标签: python dictionary

我在python中有这本词典

d={1: 'a', 2: 'b', 3: 'c'}

用d [1]我得到了

>>> d[1]
'a'

我怎样才能让钥匙对应一个值?
例如:'a'get 1

5 个答案:

答案 0 :(得分:7)

您想要反转映射:

this question

所示
dict((v,k) for k, v in map.iteritems())

答案 1 :(得分:4)

如果您正在寻找只有一个值,那么可能有更快的方法。如果您确定您的词典中存在该值,那么:

my_value = 'a'
return next(key for key, value in d.iteritems() if value == my_value)

如果您不确定是否可以获得结果,可以尝试/除了捕捉StopIteration例外情况。

此处,生成字典项目直到值匹配。如果你有一个非常大的词典,在某些情况下这个技巧可能会更快。

使用大量数据:

d1={1: 1, 2: 2, 3: 8, 4: 3, 5: 6, 6: 9, 7: 17, 8: 4, 9: 20, 10: 7, 11: 15,
    12: 10, 13: 10, 14: 18, 15: 18, 16: 5, 17: 13, 18: 21, 19: 21, 20: 8,
    21: 8, 22: 16, 23: 16, 24: 11, 25: 24, 26: 11, 27: 112, 28: 19, 29: 19,
    30: 19, 3077: 36, 32: 6, 33: 27, 34: 14, 35: 14, 36: 22, 4102: 39, 38: 22,
    39: 35, 40: 9, 41: 110, 42: 9, 43: 30, 44: 17, 45: 17, 46: 17, 47: 105, 48: 12,
    49: 25, 50: 25, 51: 25, 52: 12, 53: 12, 54: 113, 1079: 50, 56: 20, 57: 33,
    58: 20, 59: 33, 60: 20, 61: 20, 62: 108, 63: 108, 64: 7, 65: 28, 66: 28, 67: 28,
    68: 15, 69: 15, 70: 15, 71: 103, 72: 23, 73: 116, 74: 23, 75: 15, 76: 23, 77: 23, 
    78: 36, 79: 36, 80: 10, 81: 23, 82: 111, 83: 111, 84: 10, 85: 10, 86: 31, 87: 31, 
    88: 18, 89: 31, 90: 18, 91: 93, 92: 18, 93: 18, 94: 106, 95: 106, 96: 13, 9232: 35, 
    98: 26, 99: 26, 100: 26, 101: 26, 103: 88, 104: 13, 106: 13, 107: 101, 1132: 63, 
    2158: 51, 112: 21, 113: 13, 116: 21, 118: 34, 119: 34, 7288: 45, 121: 96, 122: 21, 
    124: 109, 125: 109, 128: 8, 1154: 32, 131: 29, 134: 29, 136: 16, 137: 91, 140: 16, 
    142: 104, 143: 104, 146: 117, 148: 24, 149: 24, 152: 24, 154: 24, 155: 86, 160: 11, 
    161: 99, 1186: 76, 3238: 49, 167: 68, 170: 11, 172: 32, 175: 81, 178: 32, 179: 32, 
    182: 94, 184: 19, 31: 107, 188: 107, 190: 107, 196: 27, 197: 27, 202: 27, 206: 89, 
    208: 14, 214: 102, 215: 102, 220: 115, 37: 22, 224: 22, 226: 14, 232: 22, 233: 84, 
    238: 35, 242: 97, 244: 22, 250: 110, 251: 66, 1276: 58, 256: 9, 2308: 33, 262: 30, 
    263: 79, 268: 30, 269: 30, 274: 92, 1300: 27, 280: 17, 283: 61, 286: 105, 292: 118, 
    296: 25, 298: 25, 304: 25, 310: 87, 1336: 71, 319: 56, 322: 100, 323: 100, 325: 25, 
    55: 113, 334: 69, 340: 12, 1367: 40, 350: 82, 358: 33, 364: 95, 376: 108, 
    377: 64, 2429: 46, 394: 28, 395: 77, 404: 28, 412: 90, 1438: 53, 425: 59, 430: 103, 
    1456: 97, 433: 28, 445: 72, 448: 23, 466: 85, 479: 54, 484: 98, 485: 98, 488: 23, 
    6154: 37, 502: 67, 4616: 34, 526: 80, 538: 31, 566: 62, 3644: 44, 577: 31, 97: 119, 
    592: 26, 593: 75, 1619: 48, 638: 57, 646: 101, 650: 26, 110: 114, 668: 70, 2734: 41, 
    700: 83, 1732: 30, 719: 52, 728: 96, 754: 65, 1780: 74, 4858: 47, 130: 29, 790: 78, 
    1822: 43, 2051: 38, 808: 29, 850: 60, 866: 29, 890: 73, 911: 42, 958: 55, 970: 99, 
    976: 24, 166: 112}

我比较了以下函数,以检查哪一个是最快的。

def f1():  
    return next(key for key, value in d1.iteritems() if value == 55)

def f2a():
    return [item[0] for item in d1.items() if item[1] == 55][0]

def f2b():
    return [item[0] for item in d1.iteritems() if item[1] == 55][0]

def f3():
    return dict((v, k) for k, v in d1.iteritems())[55]

tl=["f1", "f2a", "f2b", "f3"]
from timeit import Timer
cmpthese(1000, tl)

以下是结果:

f3   21182/s  --   -39%   -40%   -56%
f2a  34450/s    63%  --    -3%   -29%
f2b  35368/s    67%     3%  --   -27%
f1   48367/s   128%    40%    37%  --

f1比其他任何一个都快。

答案 2 :(得分:3)

您可以根据初始字符中的键和值创建一个新字典:

>>> d2 = dict((v, k) for k, v in d.iteritems())
>>> d2
{'a': 1, 'c': 3, 'b': 2}
>>> d2['a']
1

答案 3 :(得分:3)

key = 'a'
return [item[0] for item in self.items() if item[1] == key]

这将找到值为'a'的所有键并返回它们的列表。

答案 4 :(得分:2)

Key = next((x for x in d if d[x] == 'a'), None)

如果只存在一个相应的键,则会得到所需的结果。 (在O(n)时间?)因为它一旦找到合格的密钥就会立即退出。

Keys = [x for x in d if d[x] == 'a']

给出所有合格的密钥(在列表中)。如果没有这样的键,它返回一个空列表。 此方法称为' 列表理解'。

尽可能避免按值获取键。与从O(1)中的键访问值相比,这是非常低效的。

这意味着你想要创建一个反向映射字典,其中旧字典的键和值在新字典中作为值和键切换 - 如果你之后经常使用这种方法。