-1

I have 2 lists: a = ['5', '2', '3', '4'], and b = ['1', '6', '7', '5']. Using Python 2, how can I compare each list element in a to each element in b? (i.e. is a[0] == b[0], is a[0] == b[1], etc).

I know that I could just write out numerous if statements, but I hope that there is a more elegant way to do this.

After checking each list element, I want to know how many times a shared value was found (in my example lists above, it would be one time, '5').

EDIT: This is not a duplicate, b/c i am comparing two different lists to each other, while the possible duplicate dealt with only 1 list.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
captainGeech
  • 302
  • 1
  • 5
  • 17

5 Answers5

5

The count() method of list may help:

>>> a = ['5', '2', '3', '4']
>>> b = ['1', '6', '7', '5']
>>> for item in a:
...     print item, b.count(item)
... 
5 1
2 0
3 0
4 0
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
2

Probably faster for big inputs than eugene y's, as it only needs to iterate over b once,
instead of len(a) times:

from collections import Counter
counts = Counter(b)

for i in a:
    print(i, counts[i])
Community
  • 1
  • 1
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
1

If you are only concerned with shared values, and not with their positions or counts, convert them to set and use intersection:

>>> a = ['5','2','3','4']
>>> b = ['1','6','7','5']
>>> set(a).intersection(b)
{'5'}

If you want to retain how often the elements appear in the intersection, you can also do an intersection of collections.Counter using &

>>> a = ['5','2','3','4','1','1','6','5']
>>> b = ['1','6','7','5','5']
>>> collections.Counter(a) & collections.Counter(b)
Counter({'5': 2, '1': 1, '6': 1})

Note: This is different from the solution by @GingerPlusPlus in that it is symmetric, i.e. if 5 is present once in list a and twice in list b, then the shared count will be 1, not 2.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • 2
    "I want to know how many times a shared value was found" - sounds like OP want's a count. Intersection wouldn't work if there were two instances of the number `5` in list `b` – pyInTheSky Mar 20 '16 at 15:39
  • @pyInTheSky That is correct, i want to know if 5 is duplicated twice – captainGeech Mar 20 '16 at 15:40
  • 1
    @pyInTheSky Yes, that's one interpretation. But it could also mean the count of distinct shared values. That's why I added the introductory note. – tobias_k Mar 20 '16 at 15:40
  • that's fine, I didn't downvote you at all. Your updated solution is interesting, I had no idea the __and__ operator for Counter had some wonky magic. Pasting the help of the fn here if anyone else is curious as to how your solution works: __and__(self, other) method of collections.Counter instance Intersection is the minimum of corresponding counts. >>> Counter('abbb') & Counter('bcc') Counter({'b': 1}) – pyInTheSky Mar 20 '16 at 15:54
0
def cmp(*lists):
    lists_len_min = list(map(lambda x: len(x), lists))
    if min(lists_len_min) != max(lists_len_min):
        raise Exception("Lists must have equal length")
    iterator = iter(lists)
    last = next(iterator)
    for element in iterator:
        for i, each in enumerate(element):
            #print(i, last[i], each)
            if last[i] != each:
                return False
    else:
        return True

This function can compare as many lists you want with equal length. Just call cmp(list1, list2, list3)

DeaD_EyE
  • 433
  • 5
  • 10
0

This code will produce list of elements which is consist in both a and b list

a = [1,2,3,4]
b = [2,3,1,7]
c = [e for e in a if e in b]

It might be complex by memory in case if you use big arrays but if you plan to use this data than why not

segevara
  • 610
  • 1
  • 7
  • 18