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
[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
CodePudding user response:
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)