Search code examples
pythonskip

Python skipping negative values


So I have the following code to find a min number of 3 given values where abc,xyz,mnl are list containing values to compare as:

for q , (a,b,c) in enumerate(zip(abc,xyz,mnl)):
    print (a,b,c)
    if int(a) < int(b) & int(a) < int(c):
        print "a","\t",a
    elif int(b) < int(a) & int(b) < int(c):
        print "b","\t",b
    elif int(c) < int(a) & int(c) < int(b):
        print "c","\t",c

And I'm getting this output

('3137775', '-7589493', '-1419231')
('6199235', '-3810275', '-8726482')
('2649229', '-4119818', '3726604')
b    -4119818
('-1960710', '2758796', '9426184')
a   -1960710

The problem is it prints the min value which is 'b' on 2 iteration and skips 3 iteration before that printing. And it keeps on doing that at each iteration which contains more than one value in negative(-ve) as '-7589493', '-1419231' we can see in first iteration.

Why it can't print the min value for each iteration


Solution

  • & it's binary AND, not logical AND.

    Try this:

    for q, (a, b, c) in enumerate(zip(abc, xyz, mnl)):
        print(a, b, c)
        if int(c) > int(a) < int(b):
            print("a", "\t", a)
        elif int(c) > int(b) < int(a):
            print("b", "\t", b)
        elif int(b) > int(c) < int(a):
            print("c", "\t", c)
    

    or using AND logical operator:

    for q, (a, b, c) in enumerate(zip(abc, xyz, mnl)):
        print(a, b, c)
        if int(a) < int(b) and int(a) < int(c):
            print("a", "\t", a)
        elif int(b) < int(a) and int(b) < int(c):
            print("b", "\t", b)
        elif int(c) < int(a) and int(c) < int(b):
            print("c", "\t", c)