1

Ok, for a fun project I'm working on in order to learn some python I'm hitting a wall with what should be a basic task: I need to compare lists for the times items shared among the lists occur in each list. Using shared_items = set(alist).intersection(blist) gives me the items sharedbetwen the lists, but it does not tell me, how often those items occur in each list. I tried loops like this for example:

def the_count(alist,blist):
    c = 0
    for x in alist:
        for y in blist:
            if x == y:
                c += 1
    return c

but that doesn't do the trick.

Another attempt was to use Counter:

c = Counter(alist)
b = Counter(blist)

But trying to loop over the Counter results failed too, last try was

a = Counter(alist)
b = Counter(blist)
for key, val in a:
   if key in b:
    val1 = b[key]
    if val < val1:
        print b[key]
    else:
        print a[key]
  • Look at the following answer: https://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches – Idan Sep 20 '17 at 22:31
  • 1
    Possible duplicate of [How can I compare two lists in python and return matches](https://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches) – Idan Sep 20 '17 at 22:32
  • 1
    @Idan Meyer Thanks, but that only adresss how to get the shared elements, not how to count their occurrences in the lists. Actually I had learned from that answer how to use set intersection :) – rookie_senior Sep 21 '17 at 09:22

4 Answers4

2

You almost had it using the set intersection. Since that gives you the common elements amongst both lists, all you have to do now is loop over that and count the elements. One way could be:

list1 = [0, 1, 2, 3, 1, 2, 3, 4, 3, 2]
list2 = [1, 4, 3, 5, 2, 1, 0, 2, 7, 8]
shared = set(list1).intersection(list2)

# Now, loop over the elements and create a dictionary using a generator.
# The key will be the shared element, and the value would be a tuple
# which corresponds to the counts of the first list and the second list, respectively
counts = {num:(list1.count(num), list2.count(num)) for num in shared}

counts now contains:

{
    0: (1, 1), 
    1: (2, 2), 
    2: (3, 2), 
    3: (3, 1), 
    4: (1, 1)
}

This can further be abstracted into a function similar to:

def count_shared_elements(list1, list2):
    shared = set(list1).intersection(list2)
    return {num:(list1.count(num), list2.count(num)) for num in shared}
jrd1
  • 10,358
  • 4
  • 34
  • 51
1

Using list (dict) as jrd1 pointed comprehension:

>>> list1 = [0, 1, 2, 3, 1, 2, 3, 4, 3, 2]
>>> list2 = [1, 4, 3, 5, 2, 1, 0, 2, 7, 8]
>>> {i:(list1.count(i), list2.count(i)) for i in set(list1) & set(list2)}
{0: (1, 1), 1: (2, 2), 2: (3, 2), 3: (3, 1), 4: (1, 1)}
  • 1
    This is not a list comprehension - it's actually a dictionary comprehension. Also, using the bitwise `&` operator is actually slower (only marginally, though) in this case as each list has to be converted to a set before any comparison can be done. Compare that functionality to using a set intersection which only requires at least one `set` variable. – jrd1 Sep 21 '17 at 16:59
  • 1
    Thanks for the comment, I already correct to dict as you point, on my Moto Z the bench was very marginally indeed, running 10.000 times: my code --> 2.2313189506530762 your code-> 2.1449000835418701 – Fabio Teixeira Oct 04 '17 at 00:18
-1

Take a look at the answers linked in the question comments, another way to do this would be like this:

for a in alist:
    c+= blist.count(a)
zython
  • 1,176
  • 4
  • 22
  • 50
-1

Best way is to get the unique items from two lists and check the count of those distinct numbers in each list.

for distinct_num in set(alist + blist):
    print(alist.count(distinct_num))
    print(blist.count(distinct_num))
  • Your code has some minor errors - the first `print` statement is missing a close parentheses, and the second `print` and `count` statements are missing those closing parentheses as well. – jrd1 Sep 21 '17 at 17:01
  • You should be aware that this only counts all the unique elements of the *union* of the two lists, not the common elements! – jrd1 Sep 21 '17 at 19:45