简体   繁体   English

从对象列表中获取属性的最大值python

[英]Getting the max value of attributes from a list of objects python

I have data like id, start_time, duration, end_time我有像 id、start_time、duration、end_time 这样的数据

data = (
    <data 1 10 6.5 16.5 datacode>,
<data 2 12 7 19 datacode>,
<data 3 14 5 19 datacode>,
<data 4 9 7 16 datacode>,
)

Here i have to find end_time max list object and if end_time has more then one list objects在这里我必须找到 end_time 最大列表对象,如果 end_time 有多个列表对象

2  12 7 19 ->  
3  14 5 19 -> 

i have to consider late start_time list object to modify like add 20-end_time(20-19) = 1 and add to duration 5 to 6我必须考虑延迟 start_time 列表对象进行修改,例如添加 20-end_time(20-19) = 1 并添加到持续时间 5 到 6

3 14 5 19 to update like 3 14 6 20 

Please need help to find the solution请需要帮助以找到解决方案

[d for d in data if d.end_time == max(d.end_time for d in data)]

You should evaluate the maximum value outside of the list comprehension so it's not re-evaluated on each iteration.您应该评估列表理解之外的最大值,这样就不会在每次迭代时重新评估它。

end_times = [d.end_time for d in data if d.end_time]
# max breaks when there's a None value in the iterable
max_value = max(end_times) if end_times else None
# This will still work if max_value is None, I'm not sure if that's what you need.
values = [d for d in data if d.end_time == max_value]

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

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