Python-拆分列表以获取最小值和最大值

时间:2019-04-14 19:10:29

标签: python list split

我有一个包含2个元素的列表,第一个元素是时间戳,第二个元素是成本,它是10个列表,我想做的就是简单地按最小值和最大值排序,并显示它的时间戳以查看一天中最高或最低成本发生的时间。

所以这样的最大值是['280.88000000'],但我想知道发生在一天的什么时间,所以我需要日期['280.88000000',2019-04-13 13:42:00] ,它不需要在列表中,它可以是字符串或其他任何东西,只是不知道该怎么做..这是我尝试过的代码。

import time
import datetime


timencost = [[1555232400000, '278.63000000'], [1555236000000, '278.80000000'], [1555239600000, '279.25000000'], [1555243200000, '278.16000000'], [1555246800000, '283.10000000'], [1555250400000, '279.85000000'], [1555254000000, '279.93000000'], [1555257600000, '280.88000000'], [1555261200000, '278.91000000'], [1555264800000, '280.75000000']]


minn = float(min([i[-1] for i in timencost]))

maxx = float(max([i[-1] for i in timencost]))

print(minn, maxx)

dates = [i[-2] for i in timencost]

print(dates) #???????

#this is where iam stuck...

print(minn, "date:", str(datetime.datetime.fromtimestamp(float(dates/1000)).strftime('%Y-%m-%d %H:%M:%S')))

如何在我的下限旁边显示正确的日期?

3 个答案:

答案 0 :(得分:2)

您可以通过传递key参数来直接使用max()min(),该参数描述了您要如何处理相关元素。键应该是一个简单的函数,例如将正确的索引转换为float的函数

max(timencost, key=lambda x: float(x[1]))  # max based on float of index 1 of each element

# same with min
min(timencost, key=lambda x: float(x[1]))

将其放在一起可以得到类似的东西:

import datetime

timencost = [[1555232400000, '278.63000000'], [1555236000000, '278.80000000'], [1555239600000, '279.25000000'], [1555243200000, '278.16000000'], [1555246800000, '283.10000000'], [1555250400000, '279.85000000'], [1555254000000, '279.93000000'], [1555257600000, '280.88000000'], [1555261200000, '278.91000000'], [1555264800000, '280.75000000']]

minn = min(timencost, key=lambda x: float(x[1]))
maxx = max(timencost, key=lambda x: float(x[1]))

print("min", "date:", datetime.datetime.fromtimestamp(minn[0]/1000).strftime('%Y-%m-%d %H:%M:%S'))
print("max", "date:", datetime.datetime.fromtimestamp(maxx[0]/1000).strftime('%Y-%m-%d %H:%M:%S'))

答案 1 :(得分:0)

您不需要拆分列表

# First turn those strings to numbers
timencost = [(x, float(y)) for x,y in timencost]

# Sort them by cost
timencost.sort(key=lambda e: e[1])
# Get the max and min entries
min_entry = timencost[0]
max_entry = timencost[-1]

# the entries have both the cost and the time, format the dates how you want

答案 2 :(得分:0)

尝试

import datetime

def by_price(e):
  return float(e[1])


timencost = [[1555232400000, '278.63000000'], [1555236000000, '278.80000000'], [1555239600000, '279.25000000'], [1555243200000, '278.16000000'], [1555246800000, '283.10000000'], [1555250400000, '279.85000000'], [1555254000000, '279.93000000'], [1555257600000, '280.88000000'], [1555261200000, '278.91000000'], [1555264800000, '280.75000000']]

sorted_timencost = sorted(timencost,key=by_price)

print('max: {} {} '.format(sorted_timencost[0][1],datetime.datetime.fromtimestamp(sorted_timencost[0][0]/1000)))

print('min: {} {} '.format(sorted_timencost[-1][1],datetime.datetime.fromtimestamp(sorted_timencost[-1][0]/1000)))

输出

max: 278.16000000 2019-04-14 12:00:00
min: 283.10000000 2019-04-14 13:00:00