您如何在字典中找到第一个键? [英] How do you find the first key in a dictionary?

查看:253
本文介绍了您如何在字典中找到第一个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的程序从字典中打印出"banana".最简单的方法是什么?

I am trying to get my program to print out "banana" from the dictionary. What would be the simplest way to do this?

这是我的字典:

prices = {
    "banana" : 4,
    "apple" : 2,
    "orange" : 1.5,
    "pear" : 3
}

推荐答案

在实际命令有命令的Python版本中,您可以

On a Python version where dicts actually are ordered, you can do

my_dict = {'foo': 'bar', 'spam': 'eggs'}
next(iter(my_dict)) # outputs 'foo'

要订购字典,您需要Python 3.7+,或者如果您可以依靠Python 3.6上的技术在技术上有详细说明的有序排序特性,则可以使用3.6 +.

For dicts to be ordered, you need Python 3.7+, or 3.6+ if you're okay with relying on the technically-an-implementation-detail ordered nature of dicts on Python 3.6.

对于早期的Python版本,没有第一把钥匙".

For earlier Python versions, there is no "first key".

这篇关于您如何在字典中找到第一个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆