python - 如何在多层 python 字典中找到最小值,以及它的 "path"(即叶值的键列表)

标签 python sorting dictionary

我有以下格式的 python 字典:

d = 
{
  'category' :
  {
    'genre': <int_value>
  }
}

我想找到最小的<int_value> ,以及原始字典中的“路径”。

例如如果

d = 
{
  'free':
  {
    'adventure' : 23,
    'arcade' : 101,
  },
  'paid':
  {
    'arcade' : 130,
  }
}

...结果应该是("free", "adventure", 23) .

谁能想到一个单行代码?

提前致谢!

最佳答案

print min((d[c][x], c, x) for c in d for x in d[c])

并重新安排:

print min( (d[c][x], x, c) for c in d for x in d[c] )[::-1]

关于python - 如何在多层 python 字典中找到最小值,以及它的 "path"(即叶值的键列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16127706/

相关文章:

python - Django 模型对象初始化

c - 矩阵中的排序列(比较词典编排) | C

iphone - 使用数组索引对数组进行排序

python - 将 .map 与 defaultdict 一起使用

python - For循环一次添加5个数字

python - 网络摄像头 + 打开 CV Python |黑屏

javascript - 为什么数组上的js映射会修改原始数组?

python: ValueError: 加载字典结构时字符串格式错误

python - 为什么我的 shuffle 实现不正确?

Python:按添加顺序检索字典键?