【问题标题】:Equivalent of list.index() for an OrderedDict? [duplicate]等效于 OrderedDict 的 list.index()? [复制]
【发布时间】:2015-06-12 08:29:09
【问题描述】:

鉴于OrderedDict 中的项目本质上是有序的,人们可能会期望它具有类似于列表的index 函数的功能。它没有。

OrderedDict 模仿index 的最Pythonic/最简洁的方式是什么?

【问题讨论】:

标签: python dictionary ordereddictionary


【解决方案1】:

这取决于您想要index 的内容。 如果你想要一个键的索引,你可以这样做

o = OrderedDict([('toast',5.80),('waffles',2.30),('pancakes',3.99)])
print o.keys().index('waffles')

【讨论】:

  • 正是我需要的。
猜你喜欢
  • 2021-07-25
  • 2013-12-02
  • 2021-04-26
  • 2012-09-20
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 2014-08-01
相关资源
最近更新 更多