stock, , ; , , . :
: for item in food:... check stock[item] is > 0, , , stock[item]. min() count-count.
Pythonic :
shopping_list = ["banana", "orange", "apple", "apple", "banana", "apple"]
from collections import Counter
counted_list = Counter(shopping_list)
total = 0
for item, count in counted_list.items():
total += min(count, stock[item]) * prices[item]
:
sum( min(stock[item],count) * prices[item] for item,count in Counter(shopping_list).items() )
source
share