0

I have two lists, I want to compare one list with the other and get all the close matches as output for each word.

For example:

a = ['apple','python','ice-cream']
b = ['aaple','aple','phython','icecream','cat','dog','cell']

so when I pass the list 'a' i need to get 'aaple','aple','phython','icecream' as outputs from 'b'.

I used difflib.get_close_matches(word,pattern), but couldn't pass the whole list in one of the inputs.

difflib.get_close_matches('apple',b)

OUTPUT:
['aaple','aple']

How can I pass the whole list instead of just a single word?

Ramya T
  • 53
  • 1
  • 6

2 Answers2

0

The following code will create a list with all the close words:

import difflib
diff_words = []
for word in a:
    diff_words += difflib.get_close_matches(word, b)
print(diff_words)
Harshana
  • 5,151
  • 1
  • 17
  • 27
0

You can use a nested list comprehension, such as:

[close_match for word in a for close_match in difflib.get_close_matches(word, b)]
Selcuk
  • 57,004
  • 12
  • 102
  • 110