Python 通过它引用字典元素';s指数

Python 通过它引用字典元素';s指数,python,arrays,python-3.x,dictionary,indexing,Python,Arrays,Python 3.x,Dictionary,Indexing,我有一些数据数组: a = numpy.array([[ 1, 2, 3, 4], [ 10, 20, 30, 40], [100, 200, 300, 400]]) 并使用a创建词典: d = dict(zip( ['a', 'b', 'c'], a)) 因此: 正如我在Python3.x中所知道的,dictionary具有恒定的元素顺序。因此,据我所知,如果a是d中的第一个元素,它总是成为第一个元素

我有一些数据数组:

a = numpy.array([[  1,   2,   3,   4],
                 [ 10,  20,  30,  40],
                 [100, 200, 300, 400]])
并使用
a
创建
词典

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

正如我在Python3.x中所知道的,dictionary具有恒定的元素顺序。因此,据我所知,如果
a
d
中的第一个元素,它总是成为第一个元素

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

我知道我做不到
d[0]
因为其中没有
0
键。但是也许有某种方法:
d{0}
d.values[0]

Python 3中词典维护的顺序是它们实现的产物,不应该依赖于,因为它在将来可能会改变


没有方法根据字典的位置对其进行索引。

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

没有按位置索引词典的方法。

您可以访问
列表(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()       
如果您确实想要这种行为,可以使用
orderedict

λ 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()
您可以访问
列表(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()       
如果您确实想要这种行为,可以使用
orderedict

λ 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()

在Python3本身中不是常量排序,但在CPython 3中尤其如此。语言规范在这方面没有改变(afaik)。DICT只在Python3.6+中排序,我想如果我没有弄错的话,顺序是在Python3.6以上的字典中实现的。但这并不意味着可以使用索引来引用字典位置。但无论如何,如果顺序对您很重要,
字典
不是一条路。字典仍然没有语义顺序。在Python 3中不是常量顺序,特别是在CPython 3中。语言规范在这方面没有改变(afaik)。DICT只在Python3.6+中排序,我想如果我没有弄错的话,顺序是在Python3.6以上的字典中实现的。但这并不意味着可以使用索引来引用字典位置。但是无论如何,如果顺序对你来说很重要,
字典
不是一个好办法。字典仍然不是按语义排序的。谢谢你的扩展答案!在Python3.6中检查
dict
order会很有趣,但无论如何,我不会以这种方式使用它们。另外,您是否知道
OrderedDict
有一些缺点或限制(例如,它们比
dict
慢)?或者我可以在没有任何额外预防措施的情况下将它们用作dict
dict
?谢谢您的扩展回答!在Python3.6中检查
dict
order会很有趣,但无论如何,我不会以这种方式使用它们。另外,您是否知道
OrderedDict
有一些缺点或限制(例如,它们比
dict
慢)?或者我可以在没有任何额外预防措施的情况下将它们用作dict