简体   繁体   English

如何在Dict {key:List}中找到最小值?

[英]How to find the minimum value in a Dict{key:List}?

I am trying to find the minimum flight price in a dictionary that looks like: 我正在尝试在如下字典中找到最低航班价格:

{datetime.date(2018, 12, 16): ['KL', 'AMS', '59.84'],
  datetime.date(2018, 12, 24): ['KL', 'AMS', '59.84'],
  datetime.date(2018, 12, 25): ['KL', 'AMS', '59.84'],
  datetime.date(2018, 12, 27): ['KL', 'AMS', '59.84'],
  datetime.date(2018, 12, 18): ['KL', 'AMS', '59.84'],
  datetime.date(2018, 12, 19): ['KL', 'AMS', '59.84'],
  datetime.date(2018, 12, 20): ['KL', 'AMS', '59.84'],
  datetime.date(2018, 12, 23): ['KL', 'AMS', '59.84'],
  datetime.date(2018, 12, 17): ['AF', 'AMS', '70.24'],
  datetime.date(2018, 12, 21): ['SK', 'AMS', '97.93'],
  datetime.date(2018, 12, 26): ['SK', 'AMS', '97.93'],
  datetime.date(2018, 12, 28): ['SK', 'AMS', '97.93'],
  datetime.date(2019, 1, 2): ['SK', 'AMS', '97.93'],
  datetime.date(2019, 1, 3): ['SK', 'AMS', '97.93'],
  datetime.date(2018, 12, 29): ['AF', 'AMS', '111.64'],
  datetime.date(2018, 12, 31): ['EW', 'AMS', '127.51'],
  datetime.date(2019, 1, 1): ['EW', 'AMS', '127.51'],
  datetime.date(2018, 12, 30): ['EW', 'AMS', '147.51'],
  datetime.date(2018, 12, 22): ['KL', 'AMS', '148.84']}

The first key is a datetime, and value = list of info. 第一个键是日期时间,值=信息列表。 I'm trying to simply get the max and min price over that time series. 我试图简单地获取该时间序列的最高和最低价格。 I've tried the following: 我尝试了以下方法:

_tempPairAndPrices = {}
for item in permutations(destinations[1:],2):    
    #try:
    key_max = max(_fltDictTemp_CachedData[item].keys(),key=(lambda k: _fltDictTemp_CachedData[item][k]))
    key_min = min(_fltDictTemp_CachedData[item].keys(),key=(lambda k: _fltDictTemp_CachedData[item][k]))
    maxPx = _fltDictTemp_CachedData[item][key_max]
    minPx = _fltDictTemp_CachedData[item][key_min]

    _tempPairAndPrices[item] = {'maxPx':maxPx,'minPx':minPx}

I don't think it's properly accessing the list of information though, as it's not properly assigning max and min values... (often my min >> max) 我认为它不能正确访问信息列表,因为它没有正确分配最大值和最小值...(通常是我的最小值>>最大值)

Thanks! 谢谢!

IIUC, use min() with a key IIUC,通过key使用min()

[min(v.values(), key=lambda x: float(x[-1])) for k,v in d.items()]

Outputs 产出

[['KL', 'AMS', '59.84']]

For your edited data, just do 对于您编辑的数据,只需执行

min(d.values(), key=lambda x: float(x[-1]))

If you are just interested in getting the minimum flight price, this is one possible solution: 如果您只想获得最低的机票价格,这是一种可能的解决方案:

prices = [float(i) for i in np.array(dict.values()[0].values())[:,-1]]
min_price = min(prices)

where dict is your dictionary. dict是您的字典。 The reason I used np.array() in the list comprehension was in order to use the indexing [:,-1] since the prices are the last element of the list. 我在列表理解中使用np.array()的原因是为了使用索引[:,-1]因为价格是列表的最后一个元素。 But I see there are several prices which are equal and the minimum. 但是我看到有几个价格相等且是最低的价格。 I don't know how you plan to tackle them. 我不知道您打算如何解决这些问题。 You can of course combine the above two lines into one and write: 您当然可以将以上两行合并为一并编写:

min_price = min([float(i) for i in np.array(dict.values()[0].values())[:,-1]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM