Ex 09


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

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

di = dict()
for line in hand:
    line = line.rstrip()
    #print(line)
    wrds = line.split()
    #print(wrds)
    for w in wrds:
        #print (w)
        #print('**',w,di.get(w,-99))

        #if the key is not there the count is zero

        #oldcount = di.get(w,0)
        #print(w,'old',oldcount)
        #newcount = oldcount + 1
        #di[w] = newcount

#delete the above lines the use just this one command
        # idiom: retrieve/create/update counter
        di[w] = di.get(w,0) + 1
        #print(w,'new',di[w])

        
        #if w in di:
            #di[w] = di[w] + 1
            #print('**Existing**')
        #else :
            #di[w] = 1
            #print('**new**')
            
       # print( w,di[w])
#print(di)

#find the most common word
largest = -1
theword = None
for k,v in di.items():
    print(k,v)
    if v > largest:
        largest = v
        theword = k #remember the word was largest
print (largest,theword)

21:40

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

To in put and open file you should keep file in the same folder

if len(fname) < 1 : fname = 'words.txt' if the length of the file that input < 1 or no one input anything then open the words.txt file ( this will happen when don’t input file but hit enter)

di = dict()

define di to dict that means we create empty dictionary

for line in hand:
    line = line.rstrip()
    print(line)

define line in hand then ແຍກມັນດ້ວຍ rstrip()

 wrds = line.split()
    print(wrds)

ຄືການແບ່ງຄຳດ້ວຍຍະວ່າງ

 for w in wrds:
        print (w)
        print('**',w,di.get(w,-99))

-99 ເປັນຄ່າຕັ້ງຕົ້ນຄ້າຍໆຄືກັບ 0 ແລະເມື່ຶມັນເຫັນຄຳເກົ່າມັນກໍຈະບວກເພີ່ມອີກ 1

ເປັນການເກັບ ແລະ ນັບເອົາທຸກໆຄຳທີ່ຢູ່ໃນໄຟລ ແລ້ວເລີ່ມຕົ້ນນັບດ້ວຍ -99 ແລະເມື່ອມັນເຫັນຄຳເກົ່າຊຳອີກຄັ້ງມັນຈະນັບ 1 ແລະ ຕໍ່ໄປ


        oldcount = di.get(w,0)
        print(w,'old',oldcount)
        newcount = oldcount + 1
        di[w] = newcount

ໃຫ້ oldcount = di.get(w,0) ເພື່ອເອົາຄຳທີ່ຢູ່ໃນ di ແລ້ວເລີ່ມນັບຈາກ 0

 di[w] = di.get(w,0) + 1
        print(w,'new',di[w])

we can just use this command instead of using this print (w) print('**',w,di.get(w,-99)) oldcount = di.get(w,0) print(w,'old',oldcount) newcount = oldcount + 1 di[w] = newcount

   if w in di:
            di[w] = di[w] + 1
            print('**Existing**')
        else :
            di[w] = 1
            print('**new**')
            
        print( w,di[w])
print(di)

if w in di :

di[w] = di[w] + 1 if w in di equal w in di +1 that mean this word is already Existing then print Existing

else : di[w] = 1 print('**new**')

else print new

#find the most common word
largest = -1
theword = None
for k,v in di.items():
    print(k,v)
    if v > largest:
        largest = v
        theword = k #remember the word was largest
print (largest,theword)

we set the largest = -1 as a default then we set theword = None becuz we don’t know the most common word yet so set None as default

for k,v in di.items(): for k and v in dictionary itemes

k means keys

v means value

Here is the final output of all this code by enter the file “words.txt”

Python 3.8.9 (tags/v3.8.9:a743f81, Apr  6 2021, 13:22:56) [MSC v.1928 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:\Users\Admin\Desktop\python Exer\exercises.py 
Enter file:words.txt
Writing 1
programs 2
or 1
programming 1
is 2
a 3
very 2
creative 1
and 5
rewarding 1
activity 1
You 1
can 4
write 1
for 1
many 2
reasons 1
ranging 2
from 2
making 1
your 2
living 1
to 16
solving 1
difficult 1
data 1
analysis 1
problem 2
having 1
fun 1
helping 1
someone 1
else 1
solve 1
This 1
book 1
assumes 1
that 4
{\em 1
everyone} 1
needs 1
know 2
how 2
program 1
once 1
you 4
program, 1
will 1
figure 1
out 1
what 2
want 1
do 5
with 2
newfound 1
skills 1
We 2
are 3
surrounded 1
in 2
our 5
daily 1
lives 1
computers 5
laptops 1
cell 1
phones 1
think 1
of 5
these 1
as 1
personal 1
assistants 1
who 1
take 1
care 1
things 3
on 2
behalf 2
The 1
hardware 1
current-day 1
essentially 1
built 1
continuously 1
ask 1
us 2
the 6
question 1
What 1
would 2
like 2
me 1
next 2
Our 1
fast 1
have 1
vasts 1
amounts 1
memory 1
could 2
be 1
helpful 1
if 1
we 5
only 1
knew 2
language 2
speak 1
explain 1
computer 2
it 1
If 1
this 1
tell 1
tasks 1
were 1
reptitive 1
Interestingly, 1
kinds 2
best 1
often 1
humans 1
find 1
boring 1
mind-numbing 1
16 to
>>>

This is short becuz it print only word that are found in file we can see that it don’t print the same word and here we got the word to 16 time that means to is the most common word in this text file

Notes mentioning this note


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