python - 从字典中按位置获取键值

标签 python

我使用 python 字典 tel = {'jack': 4098, 'sape': 4139.....,'john':5147} 有没有办法获取位于索引范围之间的所有值? 例如,所有位于第 5-10 个位置的值...

谢谢, 弥敦道

最佳答案

python 字典中没有索引/位置。键/值对没有任何特定顺序。

如果你想从字典中选择特定的位置,你必须给它一些标准/顺序。

例如,按照名字的字母顺序:

sorted(tel.items())[5:11]

返回 (name, number) 的 5 元组列表,从位置 5-10 按字母顺序。

关于python - 从字典中按位置获取键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4827028/

相关文章:

python - 如何打印列表而不是列表列表

python - Python中列表到字符串转换的时间和空间复杂度

python - 在 Django 框架中使用动态模型

python - 允许 Google Cloud Compute Engine 实例将文件写入 Google Storage Bucket - Python

python - Python 中拆分函数的奇怪行为

python - 如何使用基于 numpy ndarray 的索引覆盖 getitem 方法?

python - 有没有办法让这段代码更简洁?

python - 在打印列表结束时获取 'none'

python - 如何在python中的另外两条线之间插入一条线

python - 为什么Python中导入的类属性没有改变?