Someone can help me understand this code!

Asked

Viewed 73 times

0

Can someone help me by telling me what each part of this code does!

 from collections import Counter
def jaccard_repeats(a, b):
    """Jaccard similarity measure between input iterables,
    allowing repeated elements"""
    _a = Counter(a)
    _b = Counter(b)
    c = (_a - _b) + (_b - _a)
    n = sum(c.values())
    return 1 - n/(len(a) + len(b) - n)

list1 = ['dog', 'cat', 'rat', 'cat']
list2 = ['dog', 'cat', 'rat']
list3 = ['dog', 'cat', 'mouse']     

jaccard_repeats(list1, list3)      
>>> 0.25
  • You can do a table test, see what the function Counter does in the documentation and go step by step the flow of the code and go writing on a paper so as not to get lost. It will even help you mature as a developer.

1 answer

-3

Try to use the divides and Conquer because breaking the code makes it easier to understand.

for example:

What does this first passage mean:

from Collections import Counter

Dai, you go doing line by line to understand, can include as many comments as it takes to go learning.

Better for you to go debugging your own code and understand what’s going on than for someone to tell you what’s going on.

Good luck in school!

  • This does not provide an answer to the question. To criticize or request clarification from an author, leave a comment below its publication. - Of Revision

  • You helped a lot, right?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.