简体   繁体   中英

Compare each of element in list

Is it possible to match two tuple, Compare each of element one by one, and determine where is the change happened.

Note: runA and runB output is in the loop, so it means it is not hard coded. runA and runB could be range tool01 to tool100 or tool01 only etc, depending to loop result to my query. just simply the tool is in the for loop so the tool no can be more or less.

Example result of my output #1:

runA = [(u'tool01', '21'), (u'tool02', '22'), (u'tool03', '23')]

runB = [(u'tool01', '21'), (u'tool02', '22'), (u'tool03', '22')]

Expected Result #1:

print 'there is a changes on tool03'  

Example result of my output #2:

runA = [(u'tool01', '21'), (u'tool02', '22'), (u'tool03', '23')]

runB = [(u'tool01', '20'), (u'tool02', '21'), (u'tool03', '23')]

Expected Result #2:

print 'there is a changes on tool01' 
print 'there is a changes on tool02' 

Example result of my output #3:

runA = [(u'tool01', '21'), (u'tool02', '22'), (u'tool03', '23')]

runB = [(u'tool01', '21'), (u'tool02', '22'), (u'tool03', '23')]

Expected Result #3:

print 'there is no change'

Any suggestion or basis code, thanks in advance.

Note: runA and runB output is in the loop, so it means it is not hard coded. runA and runB could be range tool01 to tool100 or tool01 only etc, depending to loop result to my query. just simply the tool is in the for loop so the tool no can be more or less.

for i in runA - 1:
  If runA[i][1] != runB[i][1]:
    print 'there is a changes in ' + runA[i][0]

This is assuming 2 things:

  1. the lists are of equal lengths
  2. the tuple names have the same indices

As one of your commenters suggest, using a dict would be easier as you can iterate through the keys and access the members with d[key]

#/bin/python
confirm_change=False

runA = [(u'tool01', '21'), (u'tool02', '22'), (u'tool03', '23')]

runB = [(u'tool01', '20'), (u'tool02', '21'), (u'tool03', '23')]

for i in runA:
    for j in runB:
        if i[0]==j[0] and not i[1]==j[1]:
            confirm_change=True
            print("there is a change in",i[0])
if confirm_change==False:
    print("There is no change")

The next function should do what you need:

def matchTuples(runA, runB):
    equal = True
    for i in range(0, len(runA) ):
        if runA[i] != runB[i]:
            equal = False
            print 'there is a change on ' + runA[i][0]
    if equal:
        'there is no change'

It iterates over the lists to check if they are equal. If there's is a change, then it prints the name of the tuple that was changed.

At the end, if no changes where detected (that is, if the variable "equal" is still True), it prints 'there is no change'.

runA = [(u'tool01', '21'), (u'tool02', '22'), (u'tool03', '23')]
runB = [(u'tool01', '21'), (u'tool02', '22'), (u'tool03', '22')]
for i in range(len(runA)):
    if runA[i] == runB[i]:
        print True
    else:
        print False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM