Hi again
I’m working with the text file and I need suggestions about 2,4,5? and mybe about 1 and 3 also.
For example I have this text.

“I love the python programming
How love the python programming?
We love the python programming
Do you like python for kids?
I like Hello World Computer Programming for Kids and Other Beginners.”

1.Read a text file and extract all word?
2.count the number of different words?
3. Count for each word the number of occurrences?
4. Display the list of words and their occurrences sorted by the alphabetical order?
5. Same list but sorted mainly by the number of occurrences, and if two words have the same number
of occurrences, sorted by the alphabetical order?

1)

f = open ("data.txt")
lines = f.readlines()
for line in lines:
    print line

3)

count = {}
for word in open('data.txt').read().split():
    if word in count:
        count[word] += 1
    else:
        count[word] = 1
for word, times in count.items():
    print "The", word, "was found", times, "times"

outPut

================================ RESTART ================================

The and was found 1 times
The do was found 1 times
The we was found 1 times
The Kids was found 1 times
The love was found 3 times

Recommended Answers

All 4 Replies

First, remove punctuation (? , . etc). Next all words should be lower() case as an upper case word is different to the computer than a lower case word. To print use the dictionary's key as below, although this code does not do any of the other above suggestions

data="""I love the python programming
 How love the python programming?
 We love the python programming
 Do you like python for kids?
 I like Hello World Computer Programming for Kids and Other Beginners."""

count = {}
for word in data.split():
    if word not in count:
        count[word] = 0
    count[word] += 1

## access the dictionary by key
for word in count:
    print "The word", word, "was found", count[word], "times"

print "\nThere are %d unique words (if Upper and lower case," % (len(count))
print "and words with punctuation, are different words)"

Thanks woooee

Any idea about 4 and 5?

Python packs a lot of power, take a close look at this:

from string import punctuation
from collections import Counter

data = """\
I love the python programming
How love the python programming?
We love the python programming
Do you like python for kids?
I like Hello World Computer Programming for Kids and Other Beginners.
"""
# convert all upper case leeters to lower case
data = data.lower()

print(data)  # test
print('-'*70)

# remove punctuation marks like .,!?
data = "".join(c for c in data if c not in punctuation)

print(data)  # test
print('-'*70)

# create a list of (word, frequency) tuples
q = [(word, freq) for word, freq in Counter(data.split()).items()]

# show q sorted by word (default is element index 0)
for word, freq in sorted(q):
    print("{:12} {}".format(word, freq))

print('-'*20)

# show q sorted by frequency (element index 1)
for word, freq in sorted(q, key=lambda x: x[1]):
    print("{:3d} {}".format(freq, word))

Woderful
Thank you so much bumsfeld.(the resul was perfect)

I like really to see also different way about 2,4 and 5

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.