Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to only count duplicates in the same index position between 2 list?

I have 3 list, I want to only count duplicates in same index position

#list example
a = [11,22]
b = [33,22]
c = [22,11]

Using a as reference and comparing b and c to a

b and a have duplicates in index position [1] so count 1 duplicate

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[11,22],[33,22]

c and a have 2 duplicates but they are in different index position so count 0

[11,22],[22,11]

My codes to compare a and b is something like this

b_list=[]
for x in range(len(a)):
    b_list.append(sum[i for i, j in zip(a, b) if i == j])
print(b_list)

b should return 1 count and c should return 0 count I want the return to be something like this

b = 1 , c = 0

>Solution :

try this

b_count, c_count = 0, 0
a = [11,22]
b = [33,22]
c = [22,11]

for i in range(len(a)):
    if a[i] == b[i]:
        b_count += 1
    if a[i] == c[i]:
        c_count += 1
print("b=", b_count, "c=", c_count)

if the lists are of variable size, you can use if condition to avoid index out of range error.

you can also use zip

b_count, c_count = 0, 0
a = [11,22]
b = [33,22]
c = [22,11]

for i in zip(a, b, c):
    if len(i) > 1 and i[0] == i[1]:
        b_count += 1
    if len(i) > 2 and i[0] == i[2]:
        c_count += 1
print("b=", b_count, "c=", c_count)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading