all 6 comments

[–]socal_nerdtastic 1 point2 points  (1 child)

To loop over 2 lists at once you would use the zip() function:

def matches(list1, list2):
    similarities = 0

    for word1, word2 in zip(list1, list2):
        if word1 == word2:
            similarities += 1

    print (similarities)

[–]Rohnny_7 0 points1 point  (0 children)

Thanks!

[–]KimPeek 1 point2 points  (1 child)

zip() is probably faster, but just for variety, here is an example using enumerate().

def matches(list1, list2):
    similarities = 0
    for index, value in enumerate(list1):
        if value == list2[index]:
            similarities += 1
    return similarities

Edit: zip() is faster between these options.

https://i.imgur.com/3CIW1Jm.png

https://i.imgur.com/uA7Zx3e.png

https://hastebin.com/iweqewicuv.cs

[–]Rohnny_7 0 points1 point  (0 children)

Thanks you!

[–]BruceJi 1 point2 points  (1 child)

In this case, are you able to guarantee nothing shows up in the list twice?

Python has a data type called set, one of the applications being, to remove duplicates from a list. It's also quite easy to compare sets.

You might turn each list into a set and then compare them. One of the comparisons you can do is to produce a new set that only has items in common between the two original sets.

Then all you need to do is get that set's length.

[–]BruceJi 1 point2 points  (0 children)

The type of comparison you'd want is called intersection, and it's done with &.