计算字符串

发布于 2024-12-04 12:24:09 字数 1480 浏览 1 评论 0原文

根据以下响应编辑问题:

我在文本文件中有一个字符串列表。我想计算这些字符串在另一个文本文件中的出现次数。

这是我在文件中的字符串示例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

予囚 2024-12-11 12:24:09
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
>>> 
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
>>> 
雪化雨蝶 2024-12-11 12:24:09
>>> 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}
>>> 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}
一腔孤↑勇 2024-12-11 12:24:09

简单的方法,未经测试,但应该有效,假设没有交叉线词

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()

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()
悟红尘 2024-12-11 12:24:09

如果您刚刚开始,请查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文