0

I want to create the following into a list comprehension:

scores = []
for x in df1_names:
    for y in df2_names:
        score = fuzz.ratio(x,y)
scores.append(score)

I have attempted the following, but I am not sure where to include the fuzz.ratio.

matrix = [[x for x in df1_names] for y in df2_names]
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Your indentation seems to be off. So your expected output is a matrix of all combinations, i.e., `[[fuzz.ratio(x,y) for x in df1_names] for y in df2_names]`? – fsimonjetz Jan 18 '23 at 11:59
  • Indeed a matrix with all possible combinations – jacktheripper Jan 18 '23 at 12:54
  • 2
    Your code is equivalent to the list comprehension `scores = [fuzz.ratio(x,y) for x in df1_names for y in df2_names]`. – mkrieger1 Jan 18 '23 at 13:08
  • 1
    Sorry, this is the correct duplicate, not the one I linked above: https://stackoverflow.com/questions/3633140/nested-for-loops-using-list-comprehension – mkrieger1 Jan 18 '23 at 13:09

0 Answers0