Pythonda Bir dict list içerisindeki max ve min elamanları bulma
Elimizde bir dict liste olduğunu düşünelim ve bu liste içerisindeki maksimum ve minum elamanları öğrenmek istiyorsak pythonda aşağıdaki kodu çalıştırmamız yeterlidir.
>>>lst = [{‘price’: 99, ‘barcode’: ‘2342355’}, {‘price’: 88, ‘barcode’: ‘2345566’},{‘price’: 77, ‘barcode’: ‘2342357’}]
>>>maxPricedItem = max(lst, key=lambda x:x[‘price’])
>>>minPricedItem = min(lst, key=lambda x:x[‘price’])
>>>maxPricedItem
{‘price’: 99, ‘barcode’: ‘2342355’}
>>>minPricedItem
{‘price’: 77, ‘barcode’: ‘2342357’}
İyi çalışmalar.
Kaynak:
[1] https://stackoverflow.com/questions/5320871/in-list-of-dicts-find-min-value-of-a-common-dict-field