Question

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. that isn't a problem . The problem is with 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. Please note that item is a list of strings. As per the docs , difflib arguments need to be hashable and item doesnt have a unique hash value. How could i possibly avoid this??

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top