-4

I am comparing string similarity between 2 lists of strings and whenever the similarity function is >0 I loose the word2 value:

list1=['aaaa','cccc','bb']
list2=['aaa','fff','v']
for word1 in list1:
        for word2 in list2:
            if (similar(word1 ,word2)>0):
                                    print(word2)

similar is Sequence Matcher:

def similar(a, b):
    s= SequenceMatcher(a,b).ratio()
    s=round(s*100,1)
    return s

If the 'similar' function is>0 then my word2 becomes ''. If i check for similar(word1,word2)==0 then my value stays right.

15
  • 1
    I know, but at least I can read it... Now that I can actually comprehend the code, I'm more comfortable casting a close vote. Commented Mar 27, 2018 at 11:56
  • 2
    Please provide a minimal reproducible example. Describe what is input, current output and expected output. Commented Mar 27, 2018 at 11:57
  • 2
    If word2 is a str object then it's impossible that calling a function with word2 as an argument will change the value of word2: str objects are immutable and functions can't rebind names in the caller's context. If word2 is not a str object, you need to explain what kind of object it is. Commented Mar 27, 2018 at 12:03
  • 1
    @theo: Then I don't believe that your code is giving you the result you say it is. You need to show enough code so that I or someone else can use it to reproduce your result, or no one will be able to help you. Commented Mar 27, 2018 at 12:08
  • 1
    How in the world did this incomprehensible mess of a question get 3 upvotes? Do I smell sock puppetry? Commented Mar 27, 2018 at 12:16

1 Answer 1

0

Here is what I understand your code to be:

from difflib import SequenceMatcher

def similar(a, b):
    s = SequenceMatcher(a, b).ratio()
    s = round(s * 100, 1)
    return s


list1=['aaaa','cccc','bb']
list2=['aaa','fff','v']

for word1 in list1:
    for word2 in list2:
        if similar(word1, word2) > 0:
            print(word2)

Running this code doesn't print anything, because your similar function returns 0 every time.

If I print the return value of similar, I get this output:

Comparing "aaaa" and "aaa", got 0.0
Comparing "aaaa" and "fff", got 0.0
Comparing "aaaa" and "v", got 0.0
Comparing "cccc" and "aaa", got 0.0
Comparing "cccc" and "fff", got 0.0
Comparing "cccc" and "v", got 0.0
Comparing "bb" and "aaa", got 0.0
Comparing "bb" and "fff", got 0.0
Comparing "bb" and "v", got 0.0

However, if I print the values of the lists afterward, none of your strings have been touched:

print('Loop finished, contents of lists:')
print('list1: {}'.format(repr(list1)))
print('list2: {}'.format(repr(list2)))
Loop finished, contents of lists:
list1: ['aaaa', 'cccc', 'bb']
list2: ['aaa', 'fff', 'v']

Demo link: https://ideone.com/0Kuikm

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answers. After some more research, i found the root cause here: stackoverflow.com/questions/4802137/… The SequenceMatcher class has this constructor: class difflib.SequenceMatcher(isjunk=None, a='', b='', autojunk=True) When using SequenceMatcher(a,b) , a was the value for isjunk and b as value for a, leaving the default '' value for b.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.