The aim of this page? is to explain the concept and usage of Python's defaultdict from the collections module, specifically wondering about the weird name. It is inspired by David Baezley's Advanced Python Mastery, see ex_2_2 > Collections.
defaultdict:
portfolio [{'name': 'AA', 'shares': 100, 'price': 32.2}, {'name': 'IBM', 'shares': 50, 'price': 91.1}, {'name': 'CAT', 'shares': 150, 'price': 83.44}, {'name': 'MSFT', 'shares': 200, 'price': 51.23}, {'name': 'GE', 'shares': 95, 'price': 40.37}, {'name': 'MSFT', 'shares': 50, 'price': 65.1}, {'name': 'IBM', 'shares': 100, 'price': 70.44}] print("### DEFAULTDICT") from collections import defaultdict print("#### Group data, e.g. find all stocks with the same name") byname = defaultdict(list) for s in portfolio: byname[s["name"]].append(s) byname # defaultdict(<class 'list'>, {'AA': [{'name': 'AA', 'shares': 100, 'price': 32.2}], 'IBM': [{'name': 'IBM', 'shares': 50, 'price': 91.1}, {'name': 'IBM', 'shares': 100, 'price': 70.44}], 'CAT': [{'name': 'CAT', 'shares': 150, 'price': 83.44}], 'MSFT': [{'name': 'MSFT', 'shares': 200, 'price': 51.23}, {'name': 'MSFT', 'shares': 50, 'price': 65.1}], 'GE': [{'name': 'GE', 'shares': 95, 'price': 40.37}]}) print('#### Find all stocks with the name "IBM"') byname["IBM"] # >>> [{'name': 'IBM', 'shares': 50, 'price': 91.1}, {'name': 'IBM', 'shares': 100, 'price': 70.44}]
Example with Lambda:
from collections import defaultdict byname = defaultdict(lambda: 0) print(byname["missing_key"]) # This will return 0
The above is the detailed content of Explaining defaultdict in Python. For more information, please follow other related articles on the PHP Chinese website!