0

))Hi all, I would like to find the closest match between two string variables using difflib, this is my case...

varA = 'plainmountain'
varB = 'skymountain'

newVarA = 'piaimauntain'

I would like to difflib to find the closest match in VarA and VarB variables (they are just variables, not a list), what if I add a third variable varC in the search??? How could I get difflib to find a single closest match.

Currently Im trying this...

varA = 'plaimountain'
varB = 'skymountain'
varAll = [varA, varB, varC]
newVarA = 'piaimauntain'

I was doint it this way...

import difflib
d = difflib.Differ()
diff = d.compare(varA, newVarA)
print '\n'.join(diff)

Now Im doing it this way...

from difflib import SequenceMatcher
result = difflib.get_close_matches(newVarA, varAll, 1, 0.7)
print result

Thanks Advanced.

Natysiu16
  • 326
  • 1
  • 6
  • 12

1 Answers1

1

If I understand your task it will help:

import difflib

varA = 'plaimountain'
varB = 'piaimauntain'
varC = 'skymountain'
varS = ['piaimauntain','sky','skymountain','dog','231']

#it parse varB by letters
best = difflib.get_close_matches(varA, varB)
print best

best = difflib.get_close_matches(varA, [varB])
print best

best = difflib.get_close_matches(varA, [varB,varC])
print best

best = difflib.get_close_matches(varA, [varB,varS])
print best
lispsil
  • 411
  • 3
  • 8
  • hi @zveryansky Thanks, yes I read from difflib docs (even though I can't do this task without creating the list varaible, is possible to do it without it?) please in your answer I don't understand why you enclose varB in brackets `[varB]`, or `[varB, varC]` or `[varB, carS]` why is that? are you creating a new list? – Natysiu16 Jul 11 '15 at 00:07
  • get_close_matches() gets at least 2 args: word and 'possibilities' is a list of sequences. If you use varB function will be compare 'plaimountain' with ['p','i','a','i','m','a','u','n','t','a','i','n']. If you use [varB] it will be compare 'plaimountain' and ['piaimauntain']. When I use [varB] I am making a list, if you cannot use list then try (varB) and it will be tuple. – lispsil Jul 11 '15 at 00:18
  • Hi @zveryansky, but there is an issue, the script returns `['plaimountain']`, but I would like to get `varA` as result instead. I mean, I would like to get the variable name of the string which is the best match, not the string itself. How can I do that? do I need to create a second comparisson? THanks Advanced. – Natysiu16 Jul 11 '15 at 02:34
  • I have tried to use a second comparisson, but I get the best match (best) as a list variable with a single item, and python doesn't allow me to compare a variable with a list variable, If Im just getting one best match, I would like to get it as a string, not as a list item. Is there any work around? Thanks Advanced. – Natysiu16 Jul 11 '15 at 03:17
  • best = difflib.get_close_matches(varA, [varB])[0] and it will be a string – lispsil Jul 11 '15 at 10:24
  • Thanks @zveryansky, great, it really solved it, thanks man, hope Im not abusing of your support, could you tell me why adding `[0]` at the end returns the bestmatch as a string variable? Thanks again. – Natysiu16 Jul 11 '15 at 14:27
  • Because function returning a list of elements ranged by bestmatch and [0] is getting top. If you using python you need to learn some basics. – lispsil Jul 11 '15 at 14:35