0

I am trying to compare the similarity between 1 number and a list of numbers, and not sure how to generate this problem?

I know how to compare the similarity of 2 inputs:

from difflib import SequenceMatcher 
def similar(a,b):
    return SequenceMatcher(None, a, b).ratio()

a = '123abc'   
b = '321321'  
similar(a,b) 

And now I want to compare the similarity/relevance between 1 number and a list of number, I tried:

A=[1,2,3,4,5,6,7]
B=2

from difflib import SequenceMatcher 
def similar(a,b):
    return SequenceMatcher(None, a, b).ratio()

similar (A,B)

And it does not give me what I want - it shows "'int' object is not iterable". I am trying to get the accuracy/confident of how the number (2) is matching with the list of A. Ideally in this case - if the number is 2, and the list if from 1-7 then similarity is 1, and if the number is 8 or 9 then similarity is 0.

Anyone has ideas of how to do it? I am a new python learner - Thank you in advance!

  • 2
    I'm not familiar with SequenceMatcher, but is there a reason you can't just create a list of a single object, or copy that object however many times if the lists need to be the same length? – dashiell Jun 25 '18 at 18:57

2 Answers2

1

Make B a list of length 1 in order to make both objects the same type so they are comparable.

A=[1,2,3,4,5,6,7]
B=[2]

from difflib import SequenceMatcher 
def similar(a,b):
    return SequenceMatcher(None, a, b).ratio()

similar (A,B)
0

Have you tried replacing:

B=2

with:

B=[2]

?

Cheers.

Madoo
  • 115
  • 6