大佬教程收集整理的这篇文章主要介绍了通过索引访问collections.OrderedDict中的项目,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果是orderedDict()
,则可以通过获取(key,value)对的元组的索引来轻松访问元素,如下所示
>>> import collections
>>> d = collections.orderedDict()
>>> d['foo'] = 'python'
>>> d['bar'] = 'spam'
>>> d.items()
[('foo', 'python'), ('bar', 'spam')]
>>> d.items()[0]
('foo', 'python')
>>> d.items()[1]
('bar', 'spam')
Dict.items
将返回一个可迭代的Dict视图对象而不是一个列表。我们需要将调用包装到一个列表上,以使建立索引成为可能
>>> items = List(d.items())
>>> items
[('foo', 'python'), ('bar', 'spam')]
>>> items[0]
('foo', 'python')
>>> items[1]
('bar', 'spam')
@H_673_0@解决方法
以上是大佬教程为你收集整理的通过索引访问collections.OrderedDict中的项目全部内容,希望文章能够帮你解决通过索引访问collections.OrderedDict中的项目所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。