查找一系列dicts中的最小值

Viv*_*dey 2 python

我有一个如下数组:

people = [{'node': 'john', 'dist': 3}, 
          {'node': 'mary', 'dist': 5}, 
          {'node': 'alex', 'dist': 4}]
Run Code Online (Sandbox Code Playgroud)

我想计算所有'dist'键的最小值.例如,在上面的例子中,答案是3.

我写了以下代码:

min = 99999
for e in people:
    if e[dist] < min:
        min = e[dist]
print "minimum is " + str(min)
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更好的方法来完成这项任务.

phi*_*hag 8

使用min功能:

minimum = min(e['dist'] for e in people)
# Don't call the variable min, that would overshadow the built-in min function
print ('minimum is ' + str(minimum))
Run Code Online (Sandbox Code Playgroud)