2016-08-08 10 views
0

pythonの部分文字列の出現に関する記事は数多くありますが、テキスト中に文字列の出現については何も見つかりません。文字列中の文字列の出現

testSTR = "Suppose you have a large text and you are trying to find the specific occurences of some words" 

#Suppose my search term is a, then I would expect the output of my program to be: 
print testSTR.myfunc("a") 
>>1 

「」全体の入力の文字列にのみ1 コンクリート参照があるので。それは同様のサブストリングをカウントするのでcount()はしないだろうので、私が得る出力は次のようになります。

print testSTR.count() 
>>3 

このような何かを行うことができますか?

+2

何を試しましたか? – levi

+0

'myfunc()'に何をしているのかを表示できますか? –

+1

具体的にはどういう意味ですか?あなたの入力には文字列 "a"の参照がたくさんありますが、おそらく_word_ "a"を検索する意味ですか? – Hamms

答えて

5

文字列を分割した後にコレクションを使用できます。

from collections import Counter 
print Counter(testSTR.split()) 

カウントは大文字と小文字を区別しないにする必要がある場合は、出力は部分文字列を変換し、特定のサブa使用のカウントを取得するには

Counter({'you': 2, 'a': 1, 'and': 1, 'words': 1, 'text': 1, 'some': 1, 'the': 1, 'large': 1, 'to': 1, 'Suppose': 1, 'are': 1, 'have': 1, 'of': 1, 'specific': 1, 'trying': 1, 'find': 1, 'occurences': 1}) 

よう

from collections import Counter 
res = Counter(testSTR.split()) 
print res['a'] 

を見てしまいますカウントする前にupper()またはlowerを使用してください。

あなたは句読点が心配な場合
res= Counter(i.lower() for i in testSTR.split()) 
1

、あなたはこれを試してみてください:

words = testSTR.split().map(lambda s: s.strip(".!?:;,\"'")) 
print "a" in words 
+0

私は句読点について心配していません、私はちょうどコード全体で** a **の数を見つけたいと思います。 – Jack

2

を私は最も簡単な方法は、正規表現を使用することであると思う:のため

import re 
testSTR = "Suppose you have a large text and you are trying to find the specific occurences of some words" 

print len(re.findall(r"\ba\b", testSTR)) 
# 1 

\ba\bチェックを " aの前後に「単語境界」があります。「単語境界」は句読点、スペース、または文字列全体の先頭または末尾です。これはもちろん、あなたが望むものでない限り、空白で分割するよりも便利です。

関連する問題