Example:
number1 = 54378
number2 = 54379
if number1 (is similar to) number2:
print (number1 + " " + number2)
input("what to do")
I would like to compare between these two numbers, and let the program notify me when this kind of (between number1 and number2 ) similarity happens.
I would like the solution to be flexible with some room for more similarities (_ust the first digit is different).
BTW, I am using Python 3.X
You can use difflib
for this:
>>> from difflib import SequenceMatcher
>>> number1 = 54378
>>> number2 = 54379
>>> SequenceMatcher(None, str(number1), str(number2)).ratio()
0.80000000000000004
After creating a SequenceMatcher
object with string representations of their numbers, use ratio()
(or quick_ratio()
or real_quick_ratio()
if speed is an issue) to get a similarity rating between 0 and 1.
After playing around with it a bit you can figure out what a good metric is for how similar they should be, and use it like this:
metric = 0.6 # just an example value
if SequenceMatcher(None, str(a), str(b)).ratio() > metric:
# a and b are similar