Python字典索引的值

时间:2013-04-23 02:14:43

标签: python dictionary indexing key key-value

我目前正在尝试从字典中获取值的索引,并且不确定要尝试的内容。

我的字典是:

    midi2notes = {                                  
        'c':("0","12","24","36","48","60","72","84","96","108","120"),
        'des':("1","13","25","37","49","61","73","85","97","109","121"),
        'd':("2","14","26","38","50","62","74","86","98","110","122"),
        'ees':("3","15","27","39","51","63","75","87","99","111","123"),
        'e':("4","16","28","40","52","64","76","88","100","112","124"),
        'f':("5","17","29","41","53","65","77","89","101","113","125"),
        'ges':("6","18","30","42","54","66","78","90","102","114","126"),
        'g':("7","19","31","43","55","67","79","91","103","115","127"),
        'aes':("8","20","32","44","56","68","80","92","104","116"),
        'a':("9","21","33","45","57","69","81","93","105","117"),
        'bes':("10","22","34","46","58","70","82","94","106","118"),
        'b':("11","23","35","47","59","71","83","95","107","119")
    }

示例:我想从key-'c'获取值 - “60”的索引。哪个应该是5.什么是最有效/最快的方式。

3 个答案:

答案 0 :(得分:7)

我愿意:

midi2notes['c'].index('60')

如果找不到索引,这将引发ValueError,但如果数字通常在元组中,则很难超越优化的C后端。

>>> midi2notes = {                                  
...         'c':("0","12","24","36","48","60","72","84","96","108","120"),
...         'des':("1","13","25","37","49","61","73","85","97","109","121"),
...         'd':("2","14","26","38","50","62","74","86","98","110","122"),
...         'ees':("3","15","27","39","51","63","75","87","99","111","123"),
...         'e':("4","16","28","40","52","64","76","88","100","112","124"),
...         'f':("5","17","29","41","53","65","77","89","101","113","125"),
...         'ges':("6","18","30","42","54","66","78","90","102","114","126"),
...         'g':("7","19","31","43","55","67","79","91","103","115","127"),
...         'aes':("8","20","32","44","56","68","80","92","104","116"),
...         'a':("9","21","33","45","57","69","81","93","105","117"),
...         'bes':("10","22","34","46","58","70","82","94","106","118"),
...         'b':("11","23","35","47","59","71","83","95","107","119")
...     }
>>> midi2notes['c'].index('60')
5
>>> midi2notes['c'].index('180')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple
>>> 

答案 1 :(得分:4)

如果您需要经常这样做,而不是每次都查找索引,只需创建一个dict,将值映射回索引:

midi2notes_map = {key: {value: index for index, value in enumerate(tup)}
                  for key, tup in midi2notes.items()}

然后你可以这样做:

midi2notes_map['c']['60']

或者,或者:

midi2notes_map = {(key, value): index
                  for key, tup in midi2notes.items()
                  for index, value in enumerate(tup)}

midi2notes_map['c', 60]

每次执行查找时,这个命令的效率应该提高5倍(因为你只需要对键进行散列,而不是线性搜索10个项目的元组)......但是当然构建映射需要一些时间,所以如果你只进行几次查找,那可能就不值得了。

当然,你应该确保这个查找的性能在担心它之前确实是相关的。 (我猜你可以提出的最慢的实现,使用一个显式的嵌套循环,仍然足够快以播放MIDI序列...)如果是,请使用timeit和你的{{1}}设置一个测试实际数据和使用模式,以找出实际上最快的。

答案 2 :(得分:1)

使用enumerate()

>>> for x,y in enumerate(midi2notes['c']):
...     if y == "60":
...         print x
... 
5