开发者

Counting strings

Edited question based on response below:

I have a list of strings in a text file. I want to count the occurrences of these strings in another text file.

Here is an example of strings I have in a file

Red Car
No lake
Newjersey turnpike

Here is the text file I want to search for the strings mentioned above:

I have a red car which I drove on newjersey 
turnpike. When I took exit 39 there was no 
lake. I drove my car on muddy roads which turned my red
car into brown. Driving on Newjersey turnpike can be confusing.

The answer I am looking for is:

New开发者_如何学Gojersey turnpike 2
No lake 1
red car 2

How do I program this in python? Thanks a lot for your help!

Here is what I tried so far:

input_file_path = r'input_file.txt'
phrase_path = r'phrase_words.txt'
string_count_path =r'string_count.txt'

f = open(phrase_path,'r')
lines = f.readlines()
keys = []
for line in lines:
    key.append(line)
phrase_word = map(string.strip,map(str.lower,keys))
f.close()

dict={}
for key in phrase_words:
    dict[key]=0
f=open(input_file_path,'r')
lines = map(string.strip,map(str.lower,f.readlines()))
for w in lines:
    try:
        dict[w] += 1
    except KeyError:
        pass
f.close()

The strings are getting assigned properly, but answer isnt right..

phrase_words = ['red car', 'no lake', 'newjersey turnpike']

lines = ['i have a red car which i drove on newjersey', 'turnpike. when i took exit 39 there was no', 'lake. i drove my car on muddy roads which turned my red', 'car into brown. driving on newjersey turnpike can be confusing.']

dict = {'red car': 0, 'newjersery turnpike': 0, 'no lake': 0}


Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> teststr = '''I have a red car which I drove on newjersey 
... turnpike. When I took exit 39 there was no 
... lake. I drove my car on muddy roads which turned my red
... car into brown. Driving on Newjersey turnpike can be confusing.
... '''
>>> teststr.count('Newjersey turnpike')
1
>>> 


>>> phrase_words
['red car', 'no lake', 'newjersey turnpike']
>>> lines
['i have a red car which i drove on newjersey', 'turnpike. when i took exit 39 there was no', 'lake. i drove my car on muddy roads which turned my red', 'car into brown. driving on newjersey turnpike can be confusing.']
>>> text = " ".join(lines) #join them in a str.
>>> {phrase: text.count(phrase) for phrase in phrase_words}
{'newjersey turnpike': 2, 'red car': 2, 'no lake': 1}


the trivial way, not tested, but should work, assume no crossing-line word

f = open('keys.txt','r')
lines = f.readlines()
keys = []
for line in lines:
    keys.extend(line.split())
f.close()

dict = {}
for key in keys:
    dict[key]=0    

f = open('target.txt','r')
lines = f.readlines()
for line in lines:
    l = line.split()
    for w in l:
        try:
            dict[w] += 1
        except KeyError:
            pass
f.close()


If you're just getting started, take a look at the Python Tutorial. This is a good read for people of any level of programming experience who just want to learn Python quickly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜