I want find min element of object (by days
attribute) in a list of object in Python
from operator import attrgetter
lists = json.loads("[" data "]")
print(lists)
maintenance_cycle = min(lists,key=lambda r: r.days)
console print :
[{'type': 'runtime', 'days': 1}]
error:
'dict' object has no attribute 'days'
CodePudding user response:
Accessing dictionary keys is not like accessing class methods
Try this code
maintenance_cycle = min(lists, key=lambda r: r['days'])
Or
maintenance_cycle = min(lists, key=lambda r: r.get('days'))
Instead of
maintenance_cycle = min(lists,key=lambda r: r.days)
And tell me if its not working...