<jupyter_start><jupyter_text>10.1 Tuples are immutable 10.2 Comparing tuplesPython starts by comparing the first element from each sequence. If they are equal, it goes on to the next elements, and so on.This lead to a patern called _DSU_ for __Decorate, Sort, Undecorate__.<jupyter_code>txt = "but soft what light in yonder window breaks"
words = txt.split()
t = list()
for word in words:
    t.append((len(word),word))    
t.sort(reverse=True)
print(t)

res = list()
for length, word in t:
    res.append(word)
    
print(res)<jupyter_output>[(6, 'yonder'), (6, 'window'), (6, 'breaks'), (5, 'light'), (4, 'what'), (4, 'soft'), (3, 'but'), (2, 'in')]
['yonder', 'window', 'breaks', 'light', 'what', 'soft', 'but', 'in']<jupyter_text>10.3 Tuple assignment<jupyter_code>addr = "<EMAIL>"
uname, domain = addr.split("@")
print(addr.split("@"))
print(uname)
print(domain)<jupyter_output>['monty', 'python.org']
monty
python.org<jupyter_text>10.4 Dictionaries and tuples 10.5 Mutiple assignment with dictionaries 10.6 The most common words<jupyter_code>import string
fhand = open('files/romeo-full.txt')
counts = dict()
for line in fhand:
    line = line.translate(str.maketrans("","",string.punctuation))
    line = line.lower()
    words= line.split()
    for word in words:
        counts[word]=counts.get(word,0)+1

# sort by value
lst = list()
for key,val in list(counts.items()):
    lst.append((val,key))
    
lst.sort(reverse=True)

for val,key in lst[:10]:
    print(key,val)<jupyter_output>i 61
and 42
romeo 40
to 34
the 34
thou 32
juliet 32
that 30
my 29
thee 24<jupyter_text>10.7 Using tuples as keys in dictionaries 10.8 Sequences: strings, lists, and tuples 10.9 Debugging 10.10 Glossary 10.11 Exercises<jupyter_code># exe 10.1
fname = input("Enter the file name: ")
try:
    fhand = open("files/"+fname)
except:
    print("file cannot be opened:",fname)

counts=dict()
for line in fhand:
    if not line.startswith("From "):continue
    words = line.split()
    addr = words[1]
    counts[addr] =counts.get(addr,0)+1
# print(counts)

lst = list()
for addr,count in counts.items():
    lst.append((count,addr))

lst.sort(reverse=True)

count1st,addr1st=lst[0]
print(addr1st,count1st)
# exe 10.2
fname = input("Enter the file name: ")
try:
    fhand = open("files/"+fname)
except:
    print("file cannot be opened:",fname)

counts=dict()
for line in fhand:
    if not line.startswith("From "):continue
    words = line.split()
#    print(words)
    times = words[5].split(":")
    hours = times[0]
    counts[hours] =counts.get(hours,0)+1
# print(counts)

lst = list()
for hour,count in counts.items():
    lst.append((hour,count))

lst.sort()

for hor, num in lst:
    print(hor,num)
# exe 10.3
import string
fhand = open('files/romeo-full.txt')
counts = dict()
for line in fhand:
    line = line.translate(str.maketrans("","",string.punctuation))
    line = line.translate(str.maketrans("","",string.digits))
    line = line.lower()
    words= line.split()
    for word in words:
        letters = list(word[:])
        # print(letters)
        for letter in letters:
            counts[letter]=counts.get(letter,0)+1

# sort by value
lst = list()
for key,val in list(counts.items()):
    lst.append((key,val))
    
lst.sort()

for key,val in lst:
    print(key,val)<jupyter_output>a 475
b 105
c 104
d 215
e 860
f 124
g 125
h 449
i 464
j 38
k 54
l 328
m 212
n 376
o 605
p 72
q 3
r 386
s 378
t 701
u 210
v 89
w 179
x 6
y 142
z 3
{'fraction_non_alphanumeric': 0.1504218795461158, 'fraction_numerical': 0.04597032295606634, 'mean_word_length': 4.011661807580175, 'pattern_counts': {'":': 1, '<': 14, '<?xml version=': 0, '>': 14, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '397334', 'license': 'MIT', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1294, 'n_words': 379, 'path': 'Python4Every1/Chapter 10 Tuples.ipynb', 'repo_name': 'qiyangjie/Python4Every1-KeyNotes', 'size': '3446'}