0

次の形式の文字列、辞書があります。

('The puppy likes flowers',
 {'laughter': (8.5, 0.9313),
  'flowers': (7.88, 1.1718),
  'the': (4.98, 0.9145),
  'puppy': (7.58, 1.4581),
  'died': (1.56, 1.198),
  'laugh': (9.5, 0.1),
  'flow': (2.3, 0.51),
  'likes':(5.9, 0.032),
  'like':(6.5, 0.021)    
   }
  )

各括弧は (スコア、標準偏差) に対応するタプルです。各タプルの最初の整数だけの平均をとっています。私はこれを試しました:

def score(string, d):
    if len(string) == 0:
        return 0
    string = string.lower()
    included = [d[word][0]for word in d if word in string]
    return sum(included) / len(included)

私が実行すると:

print score ('The puppy likes flower', {'laughter': (8.5, 0.9313), 'flower': 
(7.88, 1.1718), 'the':(4.98, 0.9145), 'puppy':(7.58, 1.4581), 
'died':(1.56, 1.198),'laugh': (9.5, 0.1),'flow': (2.3, 0.51)})

と:のみ'the'の平均を取得する必要がありますが、この実行中の関数にはand :も含まれています。'puppy', 'likes''flowers'4.98 + 7.88 + 5.9 + 7.58 / 4'like''flow'4.98 + 7.88 + 5.9 + + 7.58 + 6.5 + 2.3 / 6

4

5 に答える 5

2

まず、変数文字列を使用するのは良い考えではありません...しかし、ここでは問題ありません...ロジックに欠陥があります...次のように動作します

def avg(l):
    if l:
        return sum(l)/len(l)
    return 0

def score(s, d):
    return avg([d.get(x,[0])[0] for x in s.lower().split()])

sこれにより、含まれていない文字列の部分に0が追加されdます...それらを無視したい場合は、代わりに次を使用します

def score(s, d):
    return avg([d[x][0] for x in s.lower().split() if x in d])
于 2012-10-23T05:12:04.970 に答える
0

以下の関数でこの部分を取得できますが、タプルを少しきれいにすることにしました。

tuple = ('The puppy likes flowers',
 {'laughter': (8.5, 0.9313),
  'flowers': (7.88, 1.1718),
  'the': (4.98, 0.9145),
  'puppy': (7.58, 1.4581),
  'died': (1.56, 1.198),
  'laugh': (9.5, 0.1),
  'flow': (2.3, 0.51),
  'likes':(5.9, 0.032),
  'like':(6.5, 0.021)    
   }
  )

string = tuple[0]
dict = tuple[1]

関数を次のように定義します。

def score(string, dict):
    s = 0
    n = 0
    for each in string.lower().split(' '):
       if each in dict.keys():
          s += dict[each][0]
          n += 1
    average = s/n
    return average

あなたの場合:

In [43]: string
Out[43]: 'The puppy likes flowers'

In [44]: dict
Out[44]: 
{'died': (1.56, 1.198),
 'flow': (2.3, 0.51),
 'flowers': (7.88, 1.1718),
 'laugh': (9.5, 0.1),
 'laughter': (8.5, 0.9313),
 'like': (6.5, 0.021),
 'likes': (5.9, 0.032),
 'puppy': (7.58, 1.4581),
 'the': (4.98, 0.9145)}

関数の評価:

In [45]: score(string, dict)
Out[45]: 6.585
于 2012-10-23T05:05:53.260 に答える
0

Python の「in」操作を使用する代わりに、== を使用してみてください。つまり、編集済み:

string = string.split(' ') #Returns a list of word

included = [d[word][0]for word in d if word == string]
于 2012-10-23T05:15:31.313 に答える
0

最初に文字列を分割する必要があります。

splited_string = string.split()
included = [d[word][0]for word in d if word in splited_string]
于 2012-10-23T04:55:51.207 に答える
0

これまでの他の回答と同様に、この回答は入力文字列から分割された単語のスコアを辞書で検索します。これは、入力文字列の一部として辞書の単語を見つけてそれらを合計するコード例とは異なります。スコア。また、この回答のロジックは他のいくつかの回答のロジックと似ていますが、python の組み込みfilter関数を使用してよりコンパクトに表現されています。以下に示すプログラムの出力は、6.5854行で6.15333333333, None,です。6.032

w={'puppy': (7.58, 1.4581), 'likes': (5.9, 0.032), 'laugh': (9.5, 0.1), 'flow': (2.3, 0.51), 'the': (4.98, 0.9145), 'flowers': (7.88, 1.1718), 'laughter': (8.5, 0.9313), 'died': (1.56, 1.198), 'like': (6.5, 0.021)}

def score(s, d):
    v = [d[a][0] for a in filter(lambda x: x in d, s.lower().split())]
    return sum(v)/len(v) if len(v) else None

print score('the puppy likes flowers', w)
print score('the puppy likes flower', w)
print score('short stuff', w)
print score('the flowers flow like laughter', w)
于 2012-10-23T06:29:42.533 に答える