So I am working with a list that is composed with dictionaries and their corresponding values. So my question is how can I make a variable called max_value for the key inventory_numbers. An if statement that can choose the max_value. I've tried this so far:
max_value = 0 for innumber in towed_cars:
if "inventory_number" in cars:
if int(cars['inventory_number']) >= int(max_value):
max_value = int(cars['inventory_number'])
towed_cars=
[{"color":"RED",
"inventory_number":"2870803",
"make":"GMC",
"plate":"AX21690",
"state":"IL",
"style":"LL",
"tow_date":"2019-02-16T00:00:00.000",
"tow_facility_phone":"(773) 568-8495",
"towed_to_address":"10300 S. Doty"}
,
{"color":"SIL",
"inventory_number":"2870715",
"make":"CHEV",
"plate":"AV21919",
"state":"IL",
"style":"4D",
"tow_date":"2019-02-16T00:00:00.000",
"tow_facility_phone":"(773) 568-8495",
"towed_to_address":"10300 S. Doty"}]
You need a for loop to iterate over it:
max_value = 0
for car in cars:
if "inventory_number" in car:
inventory_number = int(car['inventory_number'])
if inventory_number > max_value:
max_value = inventory_number
You can also use generator comprehension
max(int(d.get('inventory_number', 0)) for d in towed_cars)
2870803
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.