EX 10


https://www.youtube.com/embed/EhQxwzyT16E

fname = input('Enter file:')
if len (fname) <1 : fname = 'words.txt'
hand = open(fname)

di = dict()
for line in hand :
    line = line.rstrip()
    wrds = line.split()
    for w in wrds:
        di[w] = di.get(w,0) + 1
#print(di)

tmp = list()
for k,v in di.items():
    #print(k,v)
    newt = (v,k)
    tmp.append(newt)
#print('Flipped',tmp)

tmp = sorted(tmp,reverse = True)
#print('sorted',tmp[:5])

for v,k in tmp[:5] :
    print(k,v)

Explane

fname = input('Enter file:')
if len (fname) <1 : fname = 'words.txt'
hand = open(fname)

Enter the file ,open it and if the length of file or don’t enter file then it will open the ‘words.txt’

di = dict()
for line in hand :
    line = line.rstrip() # split line by line
    wrds = line.split() #split words by space
    for w in wrds:
        di[w] = di.get(w,0) + 1 # word in dict = di get the word and start counting with 0 
print(di)

di = dict() Create the new empty dict

tmp = list()  # creat enpty list
for k,v in di.items(): #for key and value in dict the beame to items
    print(k,v)
    newt = (v,k) #new variable to keep the reverse key and value
    tmp.append(newt) #put the newt into the tmp
print('Flipped',tmp)
tmp = sorted(tmp,reverse = True) #sorted the key and value in list and reverse them .in here sorted can sorted a letter aswell it will display any word by A-Z
print('sorted',tmp[:5]) #print the sorted items but just the five biggest 
for v,k in tmp[:5] : 
    print(k,v)

Notes mentioning this note


ນີ້ຄືກຣາຟທີ່ລິ້ງໄປຫາທຸກໜ້າໃນ garden ນີ້ 🕸️.