私はPythonの初心者で、コーディングについてまだあまり知りません。
これは私がウィキペディアで得たものです:
2642 次
3 に答える
2
使用できますcollections.Counter
:
>>> from collections import Counter
>>> c1 = Counter(list1)
>>> c2 = Counter(list2)
>>> def rec_rank(key,dic):
... return dic[key]/float(sum(dic.values()))
...
>>> rec_rank('apple',c1)
0.3333333333333333
>>> rec_rank('apple',c2)
0.5
于 2013-05-13T10:57:22.507 に答える
1
これのことですか?
count = 0
for i in list:
if i == string:
count += 1.0
return count / len(list)
これは間違っています。
于 2013-05-13T10:54:43.967 に答える
1
len(filter(lambda x: x == 'apple', list1)) / float(len(list1))
sum(map(lambda x: x == 'apple', list1)) / float(len(list1))
reduce(lambda x, y: x + (y == 'apple'), list1, 0.0) / len(list1)
于 2013-05-13T10:56:11.617 に答える