Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按顺序迭代字典键?_Python_Python 3.x - Fatal编程技术网

Python 按顺序迭代字典键?

Python 按顺序迭代字典键?,python,python-3.x,Python,Python 3.x,我试图迭代一个字典(具体来说是在另一个字典中,但我认为这一部分并不重要),我无法让for循环按照值的放置顺序迭代这些值。我希望能够获取每本词典的第一个值。我认为在Python3.6之后,字典保持了它们的顺序(),但它们不会保持顺序 这就是我的字典的样子: count_dict = {'dessert': Counter({'cake': 1}), 'vegetable': Counter({'carrots': 1, 'beet': 1}), 'grain': Counter({'wheat':

我试图迭代一个字典(具体来说是在另一个字典中,但我认为这一部分并不重要),我无法让for循环按照值的放置顺序迭代这些值。我希望能够获取每本词典的第一个值。我认为在Python3.6之后,字典保持了它们的顺序(),但它们不会保持顺序

这就是我的字典的样子:

count_dict = {'dessert': Counter({'cake': 1}), 'vegetable': Counter({'carrots': 1, 'beet': 1}), 'grain': Counter({'wheat': 3, 'rice': 2, 'zantham': 1}), 'meat': Counter({'chicken': 2, 'pork': 1})}
这是我尝试的代码:

for it in count_dict:
        for food in count_dict[it]:
            out_dict[it] = food
return(out_dict)
我得到:

{'dessert': 'cake', 'vegetable': 'beet', 'grain': 'rice', 'meat': 'chicken'}
但需要从每个字典中获取第一个值:

{'dessert': 'cake', 'vegetable': 'carrots', 'grain': 'wheat', 'meat': 'chicken'}
我一直在堆栈上寻找答案,但找不到答案。(很抱歉,如果答案显而易见,我对Python有点陌生)

字典(在Python 3.6之后)保持其插入顺序(插入键和值的顺序),而不是排序顺序


我确实得到了答案

从集合导入计数器
伯爵:柜台({'cake':1}),'蔬菜]:柜台({'carrots':1,'甜菜]:1}),'谷物]:柜台({'Maeth':3,'rice':2,'zantham':1}),'肉]:柜台({'chicken':2,'pork':1})
new_dict={}
对于计数项()中的a、b:
new_dict[a]=已排序(b,key=lambda x:(-b[x],x))[0]
打印(新目录)
这将首先按值对内部字典进行排序,然后按键的字母顺序进行排序

排序(b,key=lambda x:(-b[x],x))
样本运行1(按字母顺序排序,因为值相同。)

输出

[“甜菜”、“胡萝卜”]
样本运行2(按字母顺序按值排序。)

b={'maeth':3,'rice':2,'zantham':1}
已排序(b,key=lambda x:(-b[x],x))
输出

[‘小麦’、‘大米’、‘桑塔姆’]

如果您严格地只想返回第一个键值,而不考虑字母键顺序或数值顺序,您可以简单地执行以下操作:

from collections import Counter

count_dict = {'dessert': Counter({'cake': 1}), 'vegetable': Counter({'carrots': 1, 'beet': 1}), 'grain': Counter({'wheat': 3, 'rice': 2, 'zantham': 1}), 'meat': Counter({'chicken': 2, 'pork': 1})}
out_dict = {}

for it in count_dict:
        for food in count_dict[it]:
            out_dict[it] = food
            break  # Break out the loop so that you only iterate over the first key in dictionary
print(out_dict)

请注意,实际输出和预期输出的键顺序相同。这就是保留顺序的含义:键保持相同的顺序。显然,在这种情况下,计数器使用的字典不会保持它们的顺序。
beet
不是第一个值,但您说您希望从中获得第一个值。谢谢,我一直在想这个问题,但不确定如何做。很抱歉要求提供更多信息,但是(-b[x]),x)部分如何在lambda函数中工作?
b[x]
为键
x
提供值<代码>排序将按值的升序对其进行排序。当我们将其更改为
-b[x]
时,最大值将是最小值。所以我们将按值的相反顺序得到它。您可以这样说,为什么不使用
reverse
参数,但我们也希望它按字母顺序和键排序。如果两个键的值相同,则
sorted
将检查键
x
,并根据键
x
对其进行排序。
from collections import Counter

count_dict = {'dessert': Counter({'cake': 1}), 'vegetable': Counter({'carrots': 1, 'beet': 1}), 'grain': Counter({'wheat': 3, 'rice': 2, 'zantham': 1}), 'meat': Counter({'chicken': 2, 'pork': 1})}
out_dict = {}

for it in count_dict:
        for food in count_dict[it]:
            out_dict[it] = food
            break  # Break out the loop so that you only iterate over the first key in dictionary
print(out_dict)