How to Compare 2 Indexes in Same List in Python

Compare two lists with index

def compare(A,B):
for i in range(len(A)):
if B[A.index(B[i])]==A[i]:
return True
print(B[A.index(B[i])],A[i]) # print to understand more
return False

Note

If the two lists don't have similar letters, this won't work, since it refers to the letters.

How to compare the index of 2 sublists' elements from a list of lists

Your indexing is not correct in general.
Try something like the following:

for i in range(1,len(R_list)):
if R_list[i][0]!=R_list[i-1][0]:
R_list[i].append(0)
else:
R_list[i].append(int(R_list[i][1])+int(R_list[i-1][1]))

For

R_list = [['1046', '10', 0], ['1047', '12', 0], ['1047', '12', 0]]

the above gives:

[['1046', '10', 0], ['1047', '12', 0, 0], ['1047', '12', 0, 24]]


Related Topics



Leave a reply



Submit