[英]TypeError: object of type 'float' has no len() , difflib
I have a function that makes use of the value of a key in dictionary .The value is a list and i iterate over that list to compare it with my sample string. 我有一个使用字典中键值的函数。该值是一个列表,我遍历该列表以将其与示例字符串进行比较。
for item in List1: #iterate over objects of list
[l3,l4] = dict2[item] #this just assigns float values to l3,l4 (not needed)
if ((distance(l1,l2,l3,l4)<200)and(difflib.SequenceMatcher(None, str1, item).ratio()>0.7)): #error here ,
str2 = item
distance
is a function that returns a float value on input of 4 float values l1,l2,l3,l4. distance
是一个在输入4个浮点值l1,l2,l3,l4时返回浮点值的函数。 that isn't a problem . 那不是问题。 The problem is with
difflib.SequenceMatcher(None, str1, item).ratio()
. 问题在于
difflib.SequenceMatcher(None, str1, item).ratio()
。
Here's the error: 这是错误:
TypeError Traceback (most recent call last)
<ipython-input-27-441e1aa01c23> in <module>()
----> 1 BLR_data_test['modified_buildings'] = BLR_data_test['extracted_building'].apply(modify_building)
C:\Anaconda\lib\site-packages\pandas\core\series.pyc in apply(self, func, convert_dtype, args, **kwds)
2021 values = lib.map_infer(values, lib.Timestamp)
2022
-> 2023 mapped = lib.map_infer(values, f, convert=convert_dtype)
2024 if len(mapped) and isinstance(mapped[0], Series):
2025 from pandas.core.frame import DataFrame
C:\Anaconda\lib\site-packages\pandas\lib.pyd in pandas.lib.map_infer (pandas\lib.c:44780)()
<ipython-input-26-f11f38a90e26> in modify_building(str1)
5 for item in List1:
6 [l3,l4] = dict2[item]
----> 7 if ((distance(l1,l2,l3,l4)<200)and(difflib.SequenceMatcher(None, str1, item).ratio()>0.7)):
8 str2 = item
9 break
C:\Anaconda\lib\difflib.pyc in ratio(self)
657
658 matches = reduce(lambda sum, triple: sum + triple[-1],
--> 659 self.get_matching_blocks(), 0)
660 return _calculate_ratio(matches, len(self.a) + len(self.b))
661
C:\Anaconda\lib\difflib.pyc in get_matching_blocks(self)
479 if self.matching_blocks is not None:
480 return self.matching_blocks
--> 481 la, lb = len(self.a), len(self.b)
482
483 # This is most naturally expressed as a recursive algorithm, but
TypeError: object of type 'float' has no len()
Str1 is a string , and i compare it with item
. Str1是一个字符串,我将它与
item
进行比较。 Please note that item
is a list of strings. 请注意,该
item
是字符串列表。 As per the docs , difflib arguments need to be hashable and item
doesnt have a unique hash value. 根据docs,difflib参数必须是可哈希的,并且
item
没有唯一的哈希值。 How could i possibly avoid this?? 我怎么可能避免这种情况?
Would casting your item to a string still do what you want? 将您的项目转换为字符串仍然可以做您想要的吗?
difflib.SequenceMatcher(None, str1, str(item)).ratio()
Because it solves the TypeError but I am not quite sure of what you're doing here and if it alters the behavior of your program. 因为它解决了TypeError,但是我不太确定您在这里做什么以及它是否改变了程序的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.