0

I have a number of correctly spelled names in a list

L_name = ['Julia', 'John', 'James', 'Jay', 'Jordan']

And I have a list of results from a form where people entered their name

L_entries = ['Julie', 'John', 'Jo', 'Jamie', 'Jamy', 'James', 'Jay', 'Jordan']

Now I want to iterate over both list, calculate the fuzz.ratio (from fuzzywuzzy) to determine how close the name entered in the form is to the ones in the list of names I know. Then I want to create a list of suggestions with the respective ratio in a second. It shoud of course have the same length as the entries and start with 0 and '' respectively for each entry.

L_ratio = [0] * len(L_entries)
L_suggestion = [''] * len(L_entries)

Now I do know that it is considered bad style to change lists in for loops and it also doesn't work for me. I will do it anyway to show what I hope to get in the end.

for n in L_name:

    for e,r,s in zip(L_entries,L_ratio,L_suggestion):
        ratio = fuzz.ratio(n, e)

        if ratio > r:
            r = ratio
            s = n

        else:
            r = r
            s = s

So basically, I want to interate over all the names I know and compare them to each of the ones entered in the form. If the name is more similar (i.e. has a higher ratio) than the one tried before, I want to take it as (new) suggestion, otherwise I keep the one before.

user9092346
  • 292
  • 2
  • 11
  • from difflib import SequenceMatcher should be suitable, refer to link - https://stackoverflow.com/questions/17388213/find-the-similarity-metric-between-two-strings – AnswerSeeker Sep 17 '19 at 07:01
  • That looks interesting but only replaces the fuzzy.ratio. My problem is in the for loops to replace list elements. – user9092346 Sep 17 '19 at 10:06

0 Answers0