<jupyter_start><jupyter_text>9.0 Basic concept of dictionariesThe association of a __key__ and a __value__ is called a __*key-value pair*__ or __*item*__. In general, the order of items in a dictionary is unpredictable.For __in__ operation in dictionaries, Python uses an algorithms called a __*hash table*__. 9.1 Dictionary as aset of counters<jupyter_code>word ='<KEY>'
d=dict()
for c in word:
    if c not in d:
        d[c]=1
    else:
        d[c]=d[c]+1
print(d)<jupyter_output>{'a': 4, 's': 1, 'j': 4, 'd': 3, 'f': 4, 'l': 3, 'k': 2}<jupyter_text>A better mehtod using __get()__ method.<jupyter_code>d = dict()
for c in word:
    d[c]=d.get(c,0)+1
print(d)<jupyter_output>{'a': 4, 's': 1, 'j': 4, 'd': 3, 'f': 4, 'l': 3, 'k': 2}<jupyter_text>9.2 Dictionaries and files 9.3 Looping and dictionaries 9.4 Advanced text parsing<jupyter_code>import string
fname = input("Enter the file name: ")
try:
    fhand = open("files/"+fname)
except:
    print("File cannot be opened: ", fname)
    exit()
    
counts = dict()
for line in fhand:
    line = line.rstrip()
    line = line.translate(line.maketrans("","",string.punctuation))
    line = line.lower()
    words = line.split()
    for word in words:
        if word not in counts:
            counts[word] = 1
        else:
            counts[word] +=1
print(counts)<jupyter_output>Enter the file name: mbox-short.txt
{'from': 326, 'stephenmarquarductacza': 8, 'sat': 12, 'jan': 352, '5': 10, '091416': 4, '2008': 352, 'returnpath': 27, 'postmastercollabsakaiprojectorg': 27, 'received': 243, 'murder': 54, 'mailumichedu': 54, '1412111490': 3, 'by': 272, 'frankensteinmailumichedu': 27, 'cyrus': 54, 'v238': 27, 'with': 194, 'lmtpa': 54, '05': 3, '0500': 219, 'xsieve': 27, 'cmu': 27, 'sieve': 27, '23': 27, 'unix': 27, 'socket': 27, 'v2212': 27, 'holesmritdumichedu': 12, '1412111479': 4, 'flawlessmailumichedu': 3, 'esmtp': 108, 'id': 190, 'm05eefr1013674': 1, '091415': 1, 'paploouhiacuk': 108, 'app1prodcollabuhiacuk': 27, '19435219184': 27, '477f90b02db2f12494': 1, '091410': 1, 'localhost': 54, '127001': 54, 'postfix': 54, '5f919bc2f2': 1, '141005': 1, '0000': 81, 'gmt': 81, 'messageid': 27, '200801051412m05eciah010327nakamurauitsiupuiedu': 1, 'mimeversion': 27, '10': 27, 'contenttransferencoding': 27, '7bit': 27, 'prodcollabuhiacuk': 27, '19435219182': 27, 'james': 27, [...]<jupyter_text>9.5 Debugging 9.6 Glossary 9.7 Exercises<jupyter_code># exe 2
fname = input("Enter the file name: ")
try:
    fhand = open("files/"+fname)
except:
    print("File cannot be opened: ", fname)
    exit()
    
counts = dict()
for line in fhand:
    if not line.startswith("From "):continue
    line = line.rstrip()
    words = line.split()
    day = words[2];
    counts[day]=counts.get(day,0)+1
print(counts)
# exe 3
fname = input("Enter the file name: ")
try:
    fhand = open("files/"+fname)
except:
    print("File cannot be opened: ", fname)
    exit()
    
counts = dict()
for line in fhand:
    if not line.startswith("From "):continue
    line = line.rstrip()
    words = line.split()
    address = words[1];
    counts[address]=counts.get(address,0)+1
print(counts)
# exe 4
fname = input("Enter the file name: ")
try:
    fhand = open("files/"+fname)
except:
    print("File cannot be opened: ", fname)
    exit()
    
counts = dict()
for line in fhand:
    if not line.startswith("From "):continue
    line = line.rstrip()
    words = line.split()
    address = words[1];
    counts[address]=counts.get(address,0)+1

bigcount = None
bigword = None
for word,count in counts.items():
    if bigcount is None or count > bigcount:
        bigword = word
        bigcount = count
        
print(bigword, bigcount)
# exe 5
fname = input("Enter the file name: ")
try:
    fhand = open("files/"+fname)
except:
    print("File cannot be opened: ", fname)
    exit()
    
counts = dict()
for line in fhand:
    if not line.startswith("From "):continue
    line = line.rstrip()
    words = line.split()
    address= words[1];
    domain = address[address.find("@")+1:]
    counts[domain]=counts.get(domain,0)+1
print(counts)<jupyter_output>Enter the file name: mbox-short.txt
{'uct.ac.za': 6, 'media.berkeley.edu': 4, 'umich.edu': 7, 'iupui.edu': 8, 'caret.cam.ac.uk': 1, 'gmail.com': 1}
{'fraction_non_alphanumeric': 0.17134765164031154, 'fraction_numerical': 0.07458107151286288, 'mean_word_length': 3.9567251461988304, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, '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': '195786', 'license': 'MIT', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1622, 'n_words': 462, 'path': 'Python4Every1/Chapter 9 Dictionaries.ipynb', 'repo_name': 'qiyangjie/Python4Every1-KeyNotes', 'size': '4253'}