0
我想一個程序來查找單詞好所有文本文件的一個目錄中的行內的平均模糊比,我收到以下異常沒有初始值:蟒蛇模糊減少()有
Traceback (most recent call last):
File "C:/Python27/hukj.py", line 28, in <module>
my_sum, my_len = reduce(lambda a, b: (a[0]+b[0], a[1]+b[1]), ((good_ratio(i), 1) for i in my_file))
TypeError: reduce() of empty sequence with no initial value
以下程序:
import os
path = r'C:\Python27'
data = {}
from fuzzywuzzy import fuzz
def good_ratio(a):
return fuzz.ratio(a, 'good')
for dir_entry in os.listdir(path):
dir_entry_path = os.path.join(path, dir_entry)
if os.path.isfile(dir_entry_path):
with open(dir_entry_path, 'r') as my_file:
my_sum, my_len = reduce(lambda a, b: (a[0]+b[0], a[1]+b[1]), ((good_ratio(i), 1) for i in my_file))
print(my_sum/my_len)
什麼可能導致此異常?
目前其細做! – adarshram