计算字符串
根据以下响应编辑问题:
我在文本文件中有一个字符串列表。我想计算这些字符串在另一个文本文件中的出现次数。
这是我在文件中的字符串示例
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}
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:
Newjersey 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}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的方法,未经测试,但应该有效,假设没有交叉线词
the trivial way, not tested, but should work, assume no crossing-line word
如果您刚刚开始,请查看 Python 教程。对于具有任何编程经验水平、只想快速学习 Python 的人来说,这都是一本很好的读物。
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.