python中的位置怎么看_如何知道项目在Python有序字典中的位置

首先,你需要阅读文档。如果打开一个Python教程,然后尝试查找有关OrderedDict的信息,您将看到以下内容:class collections.OrderedDict([items]) - Return an instance of a dict

subclass, supporting the usual dict methods. An OrderedDict is a dict

that remembers the order that keys were first inserted. If a new entry

overwrites an existing entry, the original insertion position is left

unchanged. Deleting an entry and reinserting it will move it to the

end.

New in version 2.7.

因此,如果你使用的是一个有序的字典,你不会删除键-然后'动物'将永远在你添加的位置-例如索引2。

此外,要获得“猫”的索引,您可以简单地使用:from collections import OrderedDict

d = OrderedDict((("fruit", "banana"), ("drinks", "water"), ("animal", "cat")))

d.keys()

>>> ['fruit', 'drinks', 'animal']

d.values()

>>> ['banana', 'water', 'cat']

# So

d.values().index('cat')

>>> 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值