0

I have the code which will throw the output from the given string.

inputdata = "HELLO HELLO HELLO BOBBY WHAT ARE YOU DOING"
myDict = {}
linenum = 0

for word in inputdata.split():
    if not word in myDict:
        myDict[word] = []

    myDict[word].append(linenum)


print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))

the output would be

   Word            Frequency     
 ARE           : 1              
 BOBBY         : 1              
 DOING         : 1              
 HELLO         : 3              
 WHAT          : 1              
 YOU           : 1   

But when I tried to replace the string with .txt file, Script was prompting the popup to enter the text instead of reading the data from the .txt file.

f = open(raw_input("eng.txt"), "r")
myDict = {}
linenum = 0

for word in f.split():
    if not word in myDict:
        myDict[word] = []

    myDict[word].append(linenum)


print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))
Prime
  • 3,530
  • 5
  • 28
  • 47

3 Answers3

1

f is a file resource in your example, not a string. You need to read from f. See here for example: How do I read a text file into a string variable in Python

Do something like the following to also deal with newline characters:

with open ("data.txt", "r") as myfile:
    f=myfile.read().replace('\n', '')

Also raw_input is for command prompt and is not useful in this situation.

Community
  • 1
  • 1
sunny
  • 3,853
  • 5
  • 32
  • 62
1

Since you have there:

std::istringstream inputdata("HELLO HELLO HELLO BOBBY WHAT ARE YOU DOING");

std::string word;
std::map<std::string, size_t> counts;

while (inputdata >> word)
    ++counts[word];

for (auto const &w : counts)
    std::cout << std::setw(15) << w.first << ": " << w.second << "\n";

Note that I've skipped producing/storing the line numbers since you didn't use them anyway. :-)

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Notes:

raw_input method is used to get user input from prompt

Changes to your own code:

f = open("eng.txt", "r")
myDict = {}
linenum = 0

for word in f.read().split():
    if not word in myDict:
        myDict[word] = []
    myDict[word].append(linenum)

print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))

Simplified code using set default method of dictionary

Code1:

myDict = {}
linenum = 0
with open("eng.txt", "r") as f:
    for word in f.read().split():
        myDict.setdefault(word,[]).append(linenum)

print "%-15s %-15s" %("Word", "Frequency")
for key in sorted(myDict):
    print '%-15s: %-15d' % (key, len(myDict[key]))

Sample input:

was very very afraid can you help
me
me
me

Output :

Word            Frequency      
afraid         : 1              
can            : 1              
help           : 1              
me             : 3              
very           : 2              
was            : 1              
you            : 1  
Community
  • 1
  • 1
The6thSense
  • 8,103
  • 8
  • 31
  • 65