简体   繁体   中英

Find the minimum value (excluding 0) from a dictionary

I have a dictionary titled date_of_data based on a CSV file. Within that dictionary is a key (column from the CSV) called 'total_crashes_per_thousand' .

I had tried the following to return the minimum value but realised that returns 0 which I want to exclude and find the minimum that's not 0 (could be 0.1, etc)

min_crash = min(date_of_data.get('total_crashes_per_thousand'))
print(min_crash)

I imagine I need some sort of If function but have had no success so far.

You can make use of list comprehension :

min_crash=min([x for x in date_of_data.get('total_crashes_per_thousand') if x!=0])

Now if you print min_crash you will get minimum value excluding 0

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.

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