Search code examples
pythonloopsfuzzywuzzy

for Loop over a list fuzzy match printing out match score


I have a question about a fuzzy match.

Here is the function I am trying to write:

def fuzz(x, keys):
    for i in keys:
        a = fuzz.ratio(x, keys)
    return

dataset['match'] = dataset.col1.apply(fuzz, word=['apple', 'orange', 'banana'])

How do I use a for loop (or other solution) over a list and append matching scores to dataset?

Expected output:

col1    match
banana  100
appl     80
oranges  90
ba       20
.
.
.
.

tried to for loop on a list


Solution

  • import fuzzywuzzy
    from fuzzywuzzy import fuzz
    
    def fuzz_match(x, keys):
      ratios = []
      for i in keys:
        a = fuzz.ratio(x, i)
        ratios.append(a)
      return max(ratios)
    
    dataset['match'] = dataset.col1.apply(fuzz_match, keys=['apple', 'orange', 'banana'])