<jupyter_start><jupyter_text>Calculating with Dictionaries  Problem: Perform various calculations on a dictionary of data.  Solution: Consider a dictionary that maps stock names to prices:<jupyter_code>prices = {
   'ACME': 45.23,
   'AAPL': 612.78,
   'IBM': 205.55,
   'HPQ': 37.20,
   'FB': 10.75
}<jupyter_output><empty_output><jupyter_text>To perform useful calculations, it is useful to invert the keys and values of the dictionary using `zip()`.- Example:<jupyter_code>min_price = min(zip(prices.values(), prices.keys()))
max_price = max(zip(prices.values(), prices.keys()))
min_price
max_price<jupyter_output><empty_output><jupyter_text>To rank the data, use `zip()` with `sorted()`<jupyter_code>prices_sorted = sorted(zip(prices.values(), prices.keys()))
prices_sorted<jupyter_output><empty_output><jupyter_text>- When performing data reductions on dictionary, it will only process the keys and not the values.- This can be fixed by using the `values()` method of a dictionary.This above approach is not recommended. We can have a use case where we would want information about the corresponding keys (e.g., which stock has the lowest price?)- Example:<jupyter_code>min(prices, key = lambda k : prices[k])
max(prices, key = lambda k : prices[k])<jupyter_output><empty_output><jupyter_text>To get the minimum value, an extra lookup step is needed.- Example:<jupyter_code>min_value = prices[min(prices, key = lambda k : prices[k])]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11716621253405994, 'fraction_numerical': 0.014986376021798364, 'mean_word_length': 6.272277227722772, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '93736', 'license': 'MIT', 'n_tokens_mistral': 496, 'n_tokens_neox': 467, 'n_words': 149, 'path': 'data structures & algorithms/8.ipynb', 'repo_name': 'Gth1205/py-cookbook', 'size': '1468'}