简体   繁体   中英

Comparing elements in a list in Python's for -loop

What is wrong in the method end in the code?

The method end returns always 1 although it should return 0 with the current data.

# return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums
# else return 0
# Eg the current sums "35 34 34 34" should return 0    

data = "2|15|14|4|12|6|7|9|8|10|11|5|13|3|2|16"
arra = data.split("|");

def do_row ( arra, n ):
        return arra[4*n:4 + 4*n]

def row_summa (row):
        return sum(map(int,row))

def end ( summat ):                                     # problem here!
        equality = 1 
        for i in summat[2:5]:
                print "Comparing: ", summat[1], " and ", i, ".\n"
                if summat[1] != i:
                        equality = 0 
        print equality

for i in range(0,4):
        summat = []
        summat.append( row_summa( do_row(arra,i) ) ) 
        print row_summa ( do_row(arra,i) )
        summa = 0 

end(summat)

I can't really tell what you're trying to do here, but I can certainly say why end() returns 1 instead of 0. In your last for loop, you reset summat to [] at the start of the loop, so at the end, summat only contains a single value (the one you most recently appended on). So when you ask for summat[2:5] on a list of a single item, Python returns an empty list (as there are no values in that range) - in which case there are no chances for equality to be set to zero because the loop in end never runs.

I think you may have an off-by-one error. Remember that array indexes in Python start at 0, not 1. So where you do this:

   for i in summat[2:5]:
            print "Comparing: ", summat[1], " and ", i, ".\n"
            if summat[1] != i:
                    equality = 0 

you are not looking at summat[0] at all. Try perhaps:

   for i in summat[1:4]:
            print "Comparing: ", summat[0], " and ", i, ".\n"
            if summat[0] != i:
                    equality = 0 

First off, end doesn't return 1. It returns None . It prints 1. Kind of deceptive if you're running it from the command line.

Second, when you call end , summat is equal to [34] . So this:

for i in summat[2:5]:

never even executes. It won't do anything unless summat contains at least 3 elements.

You have two problems. Initialising summat to [] inside the loop, also the off by one error Greg mentioned

data = "2|15|14|4|12|6|7|9|8|10|11|5|13|3|2|16"
arra = data.split("|");

def do_row ( arra, n ):
        return arra[4*n:4 + 4*n]

def row_summa (row):
        return sum(map(int,row))

def end ( summat ):                                     # problem here!
        equality = 1 
        for i in summat[1:]:  # 1 <=== IS THE SECOND ELEMENT
                print "Comparing: ", summat[0], " and ", i, ".\n"
                if summat[0] != i:
                        equality = 0 
        print equality

summat = []        # <=== DO THIS BEFORE THE LOOP
for i in range(0,4):
        summat.append( row_summa( do_row(arra,i) ) ) 
        print row_summa ( do_row(arra,i) )
        summa = 0 

end(summat)

You should also study this piece of code

data = "2|15|14|4|12|6|7|9|8|10|11|5|13|3|2|16"
arra = map(int,data.split("|"))
summat = [sum(arra[i:i+4]) for i in range(0,len(arra),4)]
print summat
print len(set(summat))==1

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