<jupyter_start><jupyter_text>数据结构和算法  1.1 将序列分解为单独的变量<jupyter_code># 
p = (4, 5)
x, y  = p
print(x, y)
data = ['ACME', 50, 91.1, (2012, 12, 21)]
name, shares, price, date = data
print(name, shares, price, date, sep='\n')
name, shares, price, (year, month, day) = data
print(name, shares, price, year, month, day, sep='\n')
_, shares, price, _ = data
print(shares, price, sep='\n')<jupyter_output>50
91.1<jupyter_text>1.2 从任意长度的可迭代对象中分解元素  1.2.1 问题需要从某个可迭代对象中分解出 N 个元素, 但是这个可迭代对象的长度可能超过 N, 这会导致出现 `too many values to unpack` 的异常<jupyter_code>def drop_first_last(grades):
    fisrt, *middle, last = grades
    return avg(middle)
items = [1, 10, 7, 4, 5, 9]
sum(items)
head, *tail = items<jupyter_output><empty_output><jupyter_text>1.6 在字典中的将键映射到多个值上<jupyter_code># 问题: 我们想要一个能将键(key) 映射到多个值的字典(即所谓的一键多值字典[ multidict])
# 解决方案: 字典是一种关联容器, 每个键都映射到一个单独的值上. 如果想让键映射到多个值, 
# 需要将这多个值保存到另一个容器如列表或集合中. 例如, 可能会想这样创建字典:
d = {
    'a': [1,2,3],
    'b': [4,5]
}
e = {
    'a': {1,2,3},
    'b': {4,5}
}
from collections import defaultdict
d = defaultdict(list)
d['a'].append(1)
d['a'].append(2)
d['b'].append(4)
d
e = defaultdict(set)
e['a'].add(1)
e['a'].add(2)
e['a'].add(-1)
e['b'].add(4)
e
import json
shapes = {}
shape1 = {'path':[11,33],'fill_color':'red'}
shape2 = {'path':[22,33],'fill_color':'red'}
shapes['1']=shape1
shapes['2']=shape2
for item in shapes.items():
    print(item[1]['path'].key<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2022551092318534, 'fraction_numerical': 0.04792107117688513, 'mean_word_length': 5.635514018691588, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '106327', 'license': 'MIT', 'n_tokens_mistral': 791, 'n_tokens_neox': 801, 'n_words': 134, 'path': 'structure&algorithm/structure&algorithm.ipynb', 'repo_name': 'KECB/learn', 'size': '1419'}