![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I'm pretty sure that's the easiest way to do it.
|
|
|
|
|
|
#12 | |
|
Programmer
Join Date: Feb 2005
Posts: 67
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#13 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
Now I am getting curious, and want to make a full word count. I could make two word lists. One list would only have unique words, let's say wList1. Then I could go through the original word list wList2 with wCount[k] = wList2.count(wList1[k]) with k in the range 0 to len(wList1), or something like that. However, I don't know how to make a list of unique words from the original list! Like Homer says: "Got to use all the power of my brain!"
__________________
I looked it up on the Intergnats! Last edited by Dietrich; Mar 5th, 2005 at 6:52 PM. |
|
|
|
|
|
|
#14 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
This is what I came up with, thanks for the help!
# Read the entire text from file into a string
# How many times does a particular word appear?
fileHandle = open ( 'NationalAnthemUSA.txt', 'r' )
str = fileHandle.read()
print str
fileHandle.close()
wordCount = str.count("free")
print "The word 'free' appears %d times." % (wordCount)
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#15 |
|
Newbie
Join Date: Feb 2005
Posts: 24
Rep Power: 0
![]() |
I think you can remove duplicate words easily enough using the 'set' data type. e.g.
s = "this this is just a test test" mySet = set(s.split()) # Create a set of unique strings print " ".join(mySet) This won't retain the order of the original string though (sets are unordered). |
|
|
|
|
|
#16 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
NameError: name 'set' is not defined Any idea?
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#17 |
|
Newbie
Join Date: Feb 2005
Posts: 24
Rep Power: 0
![]() |
As far as I know, in versions of Python prior to 2.4, you have to import the sets module. In version 2.4, they're built-in.
http://www.python.org/doc/2.4/whatsnew/node2.html http://www.python.org/doc/2.4/tut/no...00000000000000 |
|
|
|
|
|
#18 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Okay, thanks for the hint!
Had to import sets with Python 2.3 Works well now!
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#19 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
This is where I am with the project:
# Read the entire text from a file into a string # How many times does each word in the text appear? # Need to find a way to remove all punctuation marks! # I am using Python 2.3 import sets fileHandle = open ( 'NationalAnthemUSA.txt', 'r' ) # read entire text into a string str = fileHandle.read() print str fileHandle.close() # convert string to a set of unique words wordSet = sets.Set( str.split() ) print wordSet # march through the set word by word for word in wordSet: wordCount = str.count( word ) print word, '=', wordCount
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#20 |
|
Newbie
Join Date: Feb 2005
Posts: 24
Rep Power: 0
![]() |
You could generate a string containing all the punctuation characters by doing:
>>> import string
>>> punc = string.punctuation
>>> punc
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'Convert the original string into a list using list(str). Call this list 'l'. Then, you could use a list comprehension to build a new list containing only characters that don't appear in the string 'punc'. In other words, you're getting rid of all the punctuation characters. The following list comprehension should work: [x for x in l if x not in punc] There are other ways to do this e.g. by using the built-in 'filter' function or a simple loop. The list comprehension is short and sweet, though. The final step would be to join the list. Hope this helps! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|