Question: In Python I am looking for distributoin of words according to length. It will bring datas from a textfile, so let's say there is a

In Python I am looking for distributoin of words according to length. It will bring datas from a textfile, so let's say there is a text-file included thousands of names' list, it will be divided by each length of words.

It goes from 0 through 20 lines so I need total 21 lines to count whole characters in a textfile.

Here's a example of output

Words lengths distribution: Length 0 = n0 words Length 1 = n1 words Length 2 = n2 words Length 3 = n3 words Length 4 = n4 words Length 5 = n5 words

.... Length 20=n20 words

If there are 300 of 3 length of characters in text file

it will show like this Lengh 3=300 words above. Please help me to get this problem.

Here's my code

File=open("/Users/spongebobl/Desktop/Python/words_file.txt",'r') To_Read=File.read() #print(To_Read)

count_num=To_Read.splitlines() #Tot num of counts #print(count_num) characters_num=To_Read #Tot num of characters #print(characters_num) words = To_Read.split() #print(words) max_word=None #for storing maximum word min_word=None #for storing minimum word max_num=-1 #finding maximum number min_num=99999999999999999999 #finding and store minimum number

for word in words:

if(len(word) > max_num): #finding max word max_num = len(word) max_word = word if(len(word) < min_num): #finding min word min_num = len(word) min_word = word average = sum(len(word) for word in words) / len(words)

print("Total number of words in the file:", str (len(count_num))) print("Total number of characters in all the words in the file:" , str (len(To_Read) - To_Read.count(" "))) #prevent counting white space print("Average length of words in the file (rounded to two decimal digits):", round(average, 2) ) print("Maximum word length :",max_num) print("Minimum word length :",min_num)

print(Word length distribution:???)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!