根据以下回复编辑问题:
我在文本文件中有一个字符串列表。我想在另一个文本文件中计算这些字符串的出现次数。
以下是我在文件中的字符串示例
Red Car
No lake
Newjersey turnpike
这是我要搜索上述字符串的文本文件:
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.
我正在寻找的答案是:
Newjersey turnpike 2
No lake 1
red car 2
如何在python中编程?非常感谢你的帮助!
这是我到目前为止所尝试的内容:
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()
字符串被正确分配,但答案不对..
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}
答案 0 :(得分:1)
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
>>>
答案 1 :(得分: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}
答案 2 :(得分:0)
琐碎的方式,没有经过测试,但应该有效,假设没有交叉词
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()
答案 3 :(得分:-1)
如果您刚入门,请查看Python Tutorial。对于那些只想快速学习Python的任何编程经验水平的人来说,这是一本很好的读物。