2011-09-13 75 views
-1

根據以下回應編輯問題:計數字符串

我有一個文本文件中的字符串列表。我想在另一個文本文件中計算這些字符串的出現次數。

這裏是一個字符串的一個例子,我有一個文件

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} 
+2

你嘗試過這麼遠嗎?此外,這聽起來像功課,如果是這樣的話應該被標記。 –

+1

'str.count()'(http://docs.python.org/library/stdtypes.html#str.count) – tMC

+0

tmc..pls讀最後的風格..我試過了..把所有的東西都放在一起是我迷失的地方。 – Zenvega

回答

-1

如果你是剛剛開始,一起來看看在Python Tutorial。對於任何想要快速學習Python的編程經驗級別的人來說,這是一個很好的閱讀。

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

對不起。這不是預期的答案...謝謝你的嘗試! – Zenvega

+1

20行Python代碼僅用於計算文件中的字符串? – utdemir

+0

@ted XU:我根據你在這裏發佈的內容編輯了我的問題。你能讓我知道我要去哪裏嗎? – Zenvega

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 
>>> 
+0

這給紅色車1,因爲第二個是'紅\ ncar'。也許: '「」.join(teststr.splitlines())。count' – utdemir

+0

如果您想忽略換行符,請在計數之前將它們轉換爲所有空格。回車也是人物! – tMC

+0

@ utdemir:你能看到我上面編輯的問題,讓我知道我要去哪裏錯了嗎? – Zenvega

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}