-4

If in the first list an element is repeated, and in the second list this element also appears, this might be counted as 1 coincidence

Example:

>>> comptaCoincidencies(['verd', 'groc', 'blau', 'vermell'], ['marro', 'blau', 'blanc', 'negre']) 
0 
>>> comptaCoincidencies(['verd', 'groc', 'blau', 'vermell'], ['blanc', 'negre', 'verd', 'groc']) 
2
>>> comptaCoincidencies(['verd', 'groc', 'blau', 'vermell'], ['vermell', 'blau', 'groc', 'verd']) 
4
>>> comptaCoincidencies(['verd', 'verd', 'verd', 'verd'], ['vermell', 'blau', 'groc', 'verd']) 
1

I have done this:

def comptaCoincidencies(l1, l2):
    """
    Donades dues llistes, retorna el nombre d'elements coincidents entre le llistes
    >>> comptaCoincidencies(["verd", "verd"], ["blau", "blau"])
    0
    >>> comptaCoincidencies(["verd", "vermell"], ["verd", "blau"])
    1
    >>> comptaCoincidencies(["verd", "blau"], ["blau", "verd"])
    2
    """
    comptador = 0
    for i in range(0, len(l1)):
        if l1[i] in l2:
            comptador = comptador + 1
    return comptador
M.Arissa
  • 1
  • 2

1 Answers1

1

This will return a set of the duplicated elements

set(filter(set(lst1).__contains__, lst2))

and adding the len will calculate its length so the count of the duplicate elements

len(set(filter(set(lst1).__contains__, lst2)))

Update based on @Willem Van Onsem comment

set(lst1) & set(lst2)
taoufik A
  • 1,439
  • 11
  • 20
  • 1
    What is wrong with `set(lst1) & set(lst2)`. Tat being said, do not answer questions that show zero effort... – Willem Van Onsem Jan 04 '18 at 17:22
  • You're right bro 0 effort, but I think it's his first question maybe first steps in programming so why not help him and next time he will ask a well formed question with efforts included – taoufik A Jan 04 '18 at 17:26
  • 1
    because next time, people can make a *new* account, and pretend it is again the first time. Furthermore it gives the false impression that we answer 0-effort questions, and thus more 0-effort questions will be asked. – Willem Van Onsem Jan 04 '18 at 17:27
  • Now i fixed it, sorry i am new user and don't know much about how stackoverflow works... – M.Arissa Jan 04 '18 at 17:29
  • 1
    @WillemVanOnsem You're definitely right, now our friend knew how it works and he show us his effort, and I like the notes in your bio – taoufik A Jan 04 '18 at 17:40
  • 1
    @taoufikA: well I did not dv the Q (I only posted a link to the dv page) :-), honestly :). – Willem Van Onsem Jan 04 '18 at 17:44