【发布时间】:2015-06-12 08:29:09
【问题描述】:
鉴于OrderedDict 中的项目本质上是有序的,人们可能会期望它具有类似于列表的index 函数的功能。它没有。
为OrderedDict 模仿index 的最Pythonic/最简洁的方式是什么?
【问题讨论】:
标签: python dictionary ordereddictionary
鉴于OrderedDict 中的项目本质上是有序的,人们可能会期望它具有类似于列表的index 函数的功能。它没有。
为OrderedDict 模仿index 的最Pythonic/最简洁的方式是什么?
【问题讨论】:
标签: python dictionary ordereddictionary
这取决于您想要index 的内容。
如果你想要一个键的索引,你可以这样做
o = OrderedDict([('toast',5.80),('waffles',2.30),('pancakes',3.99)])
print o.keys().index('waffles')
【讨论】: