python - 通过索引号引用字典元素

标签 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))

所以:

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]

最佳答案

可以访问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 - 通过索引号引用字典元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46910370/

相关文章:

java - 如何在句子中查找字符串数组?

javascript - Javascript 数组中的图像 URL 是否会自动预加载到浏览器中?

python - 用 pandas 数据框中的平均值替换大小 > 1 的组

python-3.x - 在 pyqtgraph.ImageView() 上绘制线条

python - 如何在程序中将参数传递给scrapy spider?

python - ValueError : Layer expects 2 input(s), 但它在训练 CNN 时收到 1 个输入张量

python - 使用带有 try-except block 的 python "with"语句

python - 当两个异步任务访问同一个可等待对象时是否安全?

javascript - 我的阵列无法拼接

python-3.x - 如何让 Azure 文件共享自动生成不存在的目录?