字典求最小值经典案例
from collections import Iterable
import heapq as hpq
map_10 = load_map('map-10.pickle')
#show_map(map_10)
def min_openSet(openSet,dictionary):
r = None
order = hpq.nsmallest(len(dictionary),dictionary,lambda k:dictionary[k])
print("order=",order)
for i in order:
if i in openSet:
r = i
break
print("smallest in openSet is ",r)
return r
fScore = {}
fScore[5]=0.574734871592805
fScore[ 32 ]= 0.9117393095740053
fScore[ 16 ]= 0.5754198898452701
fScore[ 14 ]= 0.7029473349906332
openSet= set({32, 16, 14})
print(isinstance(fScore, Iterable))
print("smallest is:", min_openSet(openSet,fScore))
prices = {'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75}
print(min(prices, key =lambda k : prices[k]))
#result
True
order= [5, 16, 14, 32]
smallest in openSet is 16
smallest is: 16
FB