【问题标题】:python - fuzzywuzzy error - object of type float has no lenpython-fuzzywuzzy错误-float类型的对象没有len
【发布时间】:2018-12-05 11:45:56
【问题描述】:

我正在尝试使用fuzzywuzzy 库通过fuzz.ratio 函数获取2 个数据集中字符串之间的相似度分数。

虽然我经常收到以下错误:

 File "title_matching.py", line 29, in <module>
    match = match_title(title, all_titles_list, 75)
  File "title_matching.py", line 12, in match_title
    score = fuzz.ratio(title, title2)
  File "/usr/local/lib/python3.7/site-packages/fuzzywuzzy/utils.py", line 38, in decorator
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fuzzywuzzy/utils.py", line 29, in decorator
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/fuzzywuzzy/utils.py", line 45, in decorator
    if len(args[0]) == 0 or len(args[1]) == 0:
TypeError: object of type 'float' has no len()

下面是我使用库函数的模块:

def match_title(title, list_titles, min_score=0):
    # -1 score incase we don't get any matches
    max_score = -1
    # Returning empty name for no match as well
    max_name = ""
    # Iternating over all names in the other
    for title2 in list_titles:
        #Finding fuzzy match score
        score = fuzz.ratio(title, title2)
        # Checking if we are above our threshold and have a better score
        if (score > min_score) & (score > max_score):
            max_name = title2
            max_score = score
    return (max_name, max_score)

我已经通过打印检查了 title 和 list_titles 的值,它们分别是字符串和字符串列表。 由于库文件中正在生成错误,我不知道为什么会发生这种情况或如何解决它。

【问题讨论】:

    标签: python-3.x fuzzy-comparison fuzzywuzzy


    【解决方案1】:

    score = fuzz.ratio(title, title2)

    titletitle2 是浮点数而不是字符串。

    from fuzzywuzzy import fuzz
    
    print(fuzz.ratio('1', '2'))
    # 0
    print(fuzz.ratio(1.0, '2'))
      Traceback (most recent call last):
      File "main.py", line 3, in <module>
        print(fuzz.ratio(1.0, '2'))
      File "C:\Python37\lib\site-packages\fuzzywuzzy\utils.py", line 38, in decorator
        return func(*args, **kwargs)
      File "C:\Python37\lib\site-packages\fuzzywuzzy\utils.py", line 29, in decorator
        return func(*args, **kwargs)
      File "C:\Python37\lib\site-packages\fuzzywuzzy\utils.py", line 45, in decorator
        if len(args[0]) == 0 or len(args[1]) == 0:
    TypeError: object of type 'float' has no len()
    

    【讨论】:

    • 谢谢,经过大量记录后,我发现数据集中有缺失值,我不知道,因为数据集非常庞大。但是由于某种原因,每当有一个空值时,python 都会将其读取为浮点数,这让我感到困惑。
    • @iammrmehul np.nan 是一个浮点数,有多种历史和实际原因。 stackoverflow.com/questions/35323032/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2021-01-23
    • 2016-02-15
    相关资源
    最近更新 更多