简体   繁体   中英

Can't for loop through a whole list in python

I'm trying to code in python to filter a list of the first n integers that when divided have a remainder of 1. However, it looks like it's not checking every integer in my list according to the print statements I have. I have no idea why. I'm just starting to learn code, so please be patient with me. I suspect its something obvious I'm not seeing.

Below is my code I have so far:

def main():
    checkUpTo = 20
    listOfInts = list(range(checkUpTo))
    filterList = [3]
    boolList = []
    
    for d in filterList:
        print("checking list: " + str(listOfInts) + " for rem=1 when divided by " + str(d))
        for x in listOfInts:
            print("checking: " + str(x))
            isDivisible = checkIfDivisible(x,d)
            if isDivisible==False:
                listOfInts.remove(x)
        print("got list: " + str(listOfInts))

    print(listOfInts)

def checkIfDivisible(number,divisor):
    remainder = number % divisor
    print(str(number) + "%" + str(divisor) + "=" + str(remainder))
    if number % divisor == 1:
        return True
    if number % divisor != 1:
        return False

if __name__ == "__main__":
    main()

Thanks for the help

edit: my output:

checking list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] for rem=1 when divided by 3
checking: 0
0%3=0
checking: 2
2%3=2
checking: 4
4%3=1
checking: 5
5%3=2
checking: 7
7%3=1
checking: 8
8%3=2
checking: 10
10%3=1
checking: 11
11%3=2
checking: 13
13%3=1
checking: 14
14%3=2
checking: 16
16%3=1
checking: 17
17%3=2
checking: 19
19%3=1
got list: [1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19]
[1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19]

not sure why it's not checking 1,3, etc.

listOfInts.remove(x) changes the object ( listOfInts ) you're iterating over in for , which will change the number of iterations in the loop.

Consider:

l = [1, 2, 3, 4, 5]

for index, value in enumerate(l):
    print(l)
    l.remove(value)

print(index + 1) # 3, not 5.

Rather than removing elements from listOfInts , iterate over x in range(checkUpTo) , and if you find a value that is divisible by d , add it to a set .

Look at For loop, listOfInts.remove(x) removes element from list. Therefore, your list dose not have those element whose remainder is one.

 for x in listOfInts:
        print("checking: " + str(x))
        isDivisible = checkIfDivisible(x, d)
        if isDivisible == False:
             listOfInts.remove(x)

Lets understand what is hapening in for loop,

  • List contain element from 0 to 19.

  • for loop takes 0 and pass it to checkIfDivisible function.

  • 0%3=0 returns False .

  • isDivisible == False , so listOfInts.remove(x) removes element from position 0.

  • for loop now fetches next item, since we have removed item from 0 position now at 0 position we have element 1 at 1 position we have elemt 2 and so on.

  • therefore print statement doesnot display element 1.

     for x in listOfInts: print("checking: " + str(x)) isDivisible = checkIfDivisible(x, d) print(isDivisible) if isDivisible == False: print(x) print(listOfInts) listOfInts.remove(x)

    checking: 0 0%3=0 False 0 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] checking: 2 2%3=2 False 2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

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