![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
I need to read in a text file and find out how many times a particular word appears in the text. Any ideas?
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#2 |
|
Professional Programmer
|
sure, make two arrays, one that stores words, one that stores numbers, make them the same size ex.
1 - A 1 - 1 2 - B 2 - 3 .. .. n - Z n - 5 so now you go through every word in the file, and if it's already in the first array, increment the index at the second array, if it doens't exist yet, add it to the array. good luck Dizz |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
How do I separate my text from the file into words?
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#4 |
|
Professional Programmer
|
using python? no idea, but it's a common thing, search google
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Posts: 67
Rep Power: 4
![]() |
I can't give you an exact code, but you can check each letter for a space, and the slice the text from space to space and save it in a list or dictionary...
How I do not exactely know, but something like this might work: list = []
for x text:
if x == ' ':
list.append[:#till the space](There should be some way, to figure out which index it has...)
text.del[:space+1] (so that the counting restarts from zero in the next run of the loop)Then you should have all the words in a list... As soon as I get home, and have acces to a python, I will try and figure out a way... If you are faster then me, be sure to post it... ![]() Hope this helped a bit |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2005
Posts: 67
Rep Power: 4
![]() |
Here the promised code:
import string
text = 'h h h'
l = []
for x in text:
if x == ' ':
space = text.index(' ')
part = text[:space]
l.append(part)
part1 = text[:space + 1]
string.join(string.split(text, part1), '') #NO space between '' !!!!
print lIt has one problem though: It does not add the last word of the string to the list. As soon as I come up with a solution to that one, I will edit my post or post new... hope this helps ![]() Last edited by Fred; Mar 4th, 2005 at 5:26 PM. |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Feb 2005
Posts: 67
Rep Power: 4
![]() |
Well...
As always, when I am doing something, there is a better way: text.split() That does the job, and includes the last word... All that work for nothing :mad: |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2005
Posts: 24
Rep Power: 0
![]() |
Couldn't you just use the "count" method of the string type?
e.g. s = "h h h" s.count("h") (returns 3) |
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
will make it work thanks
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#10 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
works really great!! Now I just sort the list and find the duplicate words. Or does sombody have a smarter solution? thanks again!
__________________
I looked it up on the Intergnats! |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|