次のような状況があります。
str='this is the string that Luci want to parse for a dataset uci at web'
word='uci'
str.count(word)=?
独立して (単語内ではなく) 表示される 'uci' のみをカウントしたいので、出力は 2 ではなく 1 になります。
Python スクリプトが必要です。
あまりにも多くを与えることなく、re
パターンを見つけるために使用できます。特に、単語の壁で囲まれた「uci」を探すことができます。
string = 'this is the string that Luci want to parse for a dataset uci at web'
count = len(re.findall(r'[^\W]uci[\W$]', string))
または、単語以外の文字で分割し、そこでの出現を数えることもできます。
count = re.split(r'\W', string).count('uci')
これらのアプローチは両方とも 1 を返します
def count_words(str):
words = str.split()
counts = {}
for word in words:
if word in counts:
counts[word] = counts[word] + 1
else:
counts[word] = 1
return counts
count_words(str)
{'a': 1, 'web': 1, 'string': 1, 'for': 1, 'that': 1, 'this': 1, 'is': 1, 'dataset': 1, 'parse': 1, 'to': 1, 'at': 1, 'want': 1, 'the': 1, 'Luci': 1, 'uci': 1}