Home > Back-end >  need to compare item from first list to every other item in second list and return pass or fail for
need to compare item from first list to every other item in second list and return pass or fail for

Time:07-20

i have two lists

lsitA = [100,200,300]
listB = [[97,103],[103,202],[202,250]]

i'm trying to check if item in listA[x] is within a certain margin from listB[i][0] or listB[i][1] then return false, else return true. in other words i'm trying to see if lsitA[0] meets these condition either for listB[1] or listB[2] or listB[3], then return False. number of True/False must be equal to number of sub-lists in listB

this is what i have tried so far

lsitA = [100,200,300]
listB = [[97,103],[103,202],[202,250]]

def check(listB, val):
    result = []  
    
    for x in range(len(listB)):
        if listB[x][0]<=val<=listB[x][0] 4 or listB[x][1]-4<=val<=listB[x][1]:
            return result.append(print('Fail')) 
    return result.append(print('Pass'))

for i in lsitA:
    check(listB,i)

##Expected output: [Fail,Fail,Pass]

CodePudding user response:

result.append() Returns nothing, and so does print(). If you want a list containing the result, I suggest doing the followings:

def check(listB, val):
    for item in listB:
        if val in range(item[0], item[1] 1):
            return 'Fail'
    return 'Pass'

result = [check(listB, i) for i in listA]
  • Related