<jupyter_start><jupyter_text>1-1<jupyter_code>p = (4,5)
x,y = p
x
y
data = ['ACME',50,91.1,(2012,12,21)]
name,shares,price,date = data
name
date
name,shares,price,(year,month,day) = data
year
month
day
s = 'Hello'
a,b,c,d,e = s
a
_,share,price,_ = data
share
price<jupyter_output><empty_output><jupyter_text>1-2<jupyter_code>def drop_first_last(grades):
    first,*middle,last = grades
    return middle
record = ('Dave','<EMAIL>','773,555,1212','847-555,1212')
name,email,*phone_number = record
name
email
phone_number
drop_first_last([1,2,3,4,5,6])
*trailing,current = [1,2,3,4,5,6,8]
trailing
current
def sum(items):
    head,*tail = items
    return head + sum(tail) if tail else head
sum([1,2,3])<jupyter_output><empty_output><jupyter_text>1-3<jupyter_code>from collections import deque
def search(lines,pattern,history=5):
    previous_lines = deque(maxlen=history)
    for line in lines:
        if pattern in line:
            yield line,previous_lines
        previous_lines.append(line)
q = deque(maxlen = 3)
q.append(1)
q.append(2)
q.append(3)
q
q.append(4)
q
q.append(5)
q
q = deque()
q.append(1)
q.append(2)
q.append(3)
q
q.appendleft(4)
q
q.pop()
q
q.popleft()
q<jupyter_output><empty_output><jupyter_text>1-4<jupyter_code>import heapq
nums = [1,8,2,23,7,-4,18,23,42,37,2]
heapq.nlargest(3,nums)
heapq.nsmallest(3,nums)
portfolio = [
    {'name': 'IBM', 'shares': 100, 'price': 91.1},
    {'name': 'AAPL', 'shares': 50, 'price': 543.22},
    {'name': 'FB', 'shares': 200, 'price': 21.09},
    {'name': 'HPQ', 'shares': 35, 'price': 31.75},
    {'name': 'YHOO', 'shares': 45, 'price': 16.35},
    {'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap = heapq.nsmallest(3,portfolio,key=lambda s:s['price'])
cheap
heap = list(nums)
heapq.heapify(heap)
heap<jupyter_output><empty_output><jupyter_text>1-5<jupyter_code># class See 1-5.py
from class1_5 import PriorityQueue,Item
q = PriorityQueue()
q.push(Item('foo'),1)
q.push(Item('bar'),5)
q.push(Item('spam'),4)
q.push(Item('grok'),1)
q.pop()
q.pop()
q.pop()
q.pop()
## about compare in the tuple
a = Item('foo')
b = Item('bar')
a<b
a = (1,Item('foo'))
b = (2,Item('bar'))
a<b
c = (1,Item('grok'))
a<c
a = (1, 0, Item('foo'))
b = (5, 1, Item('bar'))
c = (1, 2, Item('grok'))
a<b
a<c<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2086994727592267, 'fraction_numerical': 0.06458699472759227, 'mean_word_length': 5.6, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 22, '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': '84921', 'license': 'MIT', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1023, 'n_words': 195, 'path': 'Ch01/DSA.ipynb', 'repo_name': 'GiantZwlin/PythonCookBook', 'size': '2285'}