【问题标题】:Refer to dictionary element by it's index number通过索引号引用字典元素
【发布时间】:2018-04-05 05:48:15
【问题描述】:

我有一些数据数组:

a = numpy.array([[  1,   2,   3,   4],
                 [ 10,  20,  30,  40],
                 [100, 200, 300, 400]])

并使用a 创建dictionary

d = dict(zip( ['a', 'b', 'c'], a))

所以:

print(d)
{'a': array([1, 2, 3, 4]), 'b': array([10, 20, 30, 40]), 'c': array([100, 200, 300, 400])}

据我所知,Python 3.x 字典具有恒定的元素顺序。因此,据我了解,如果 ad 中的第一个元素,它总是成为第一个。

我的问题,如果我的推理是正确的,是否有任何方法可以使用它的索引号来引用元素?

我知道我做不到 d[0] 因为里面没有 0 键。但也许有某种方式:d{0}d.values[0]

【问题讨论】:

  • 在 Python 3 本身中不是恒定的排序,但在 CPython 3 中尤其如此。语言规范在这方面没有改变(afaik)。
  • 我认为字典仅在 python 3.6+ 中排序
  • 如果我没记错的话,命令是在 Python 3.6 以上的字典中实现的。然而,这并不意味着您可以使用索引来引用字典位置。但无论如何,如果订单对您很重要,dictionaries 不是您要走的路。
  • 字典仍然没有语义排序。

标签: python arrays python-3.x dictionary indexing


【解决方案1】:

可以访问list(d.values())[0]可能会得到不同的值 每次执行脚本时(实际上,每次重新启动解释器时),所以你真的不应该这样做/依赖它。

λ python3                                                                                                                       
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32                                  
Type "help", "copyright", "credits" or "license" for more information.                                                          
>>> d = {'a': 1, 'b': 2}                                                                                                        
>>> list(d.values())[0] 
2                                                                                                                                                                                                           
>>> exit()                                                                                                                      

λ python3                                                                                                                       
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32                                  
Type "help", "copyright", "credits" or "license" for more information.                                                          
>>> d = {'a': 1, 'b': 2}                                                                                                        
>>> list(d.values())[0]                                                                                                         
1                                                                                                                               
>>> exit()       

如果你真的想要这种行为,你可以使用OrderedDict

λ python3
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import OrderedDict
>>> d = OrderedDict({'a': 1, 'b': 2})
>>> list(d.values())[0]
1
>>> exit()

λ python3
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import OrderedDict
>>> d = OrderedDict({'a': 1, 'b': 2})
>>> list(d.values())[0]
1
>>> exit()

【讨论】:

  • 感谢您的扩展回答!在 Python 3.6 中检查 dict 顺序会很有趣,但无论如何我永远不会以这种方式使用它们。附言你知道OrderedDict 是否有一些缺点或限制(例如,它们比dict 慢)?或者我可以将它们用作dict 而无需任何额外的预防措施?
【解决方案2】:

Python 3 中字典维护的顺序是其实现的产物,不应依赖,因为它可能会在未来发生变化。

没有根据位置索引字典的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2014-07-06
    • 2012-03-03
    • 2021-04-02
    • 2013-12-03
    • 2015-08-15
    • 2023-03-14
    相关资源
    最近更新 更多