简体   繁体   中英

For Loop to While Loop using IN for while loops

I am quite new to Python 2.7 so I had a couple of questions regarding using for loops to while loops.

For example: I am writing this definition

def missingDoor(trapdoor,roomwidth,roomheight,step):        
    safezone = []
    hazardflr = givenSteps(roomwidth,step,True)
    safetiles = []

    for m in hazardflr:
        safetiles.append((m,step))
        i = 0
        while i < len(safetiles):
            nextSafe = safetiles[i]
            if knownSafe(roomwidth, roomheight, nextSafe[0], nextSafe[1]):
                if trapdoor[nextSafe[0]/roomwidth][nextSafe[0]%roomwidth] is "0":
                    if nextSafe[0] not in safezone:
                        safezone.append(nextSafe[0])
                    for e in givenSteps(roomwidth,nextSafe[0],True):
                        if knownSafe(roomwidth, roomheight, e, nextSafe[0]):
                            if trapdoor[e/roomwidth][e%roomwidth] is "0" and (e,nextSafe[0]) not in safetiles:
                                safetiles.append((e,nextSafe[0]))
            i += 1  
    return sorted(safezone)

I am trying to turn all the for loops to a while loops, so this is currently what I have written so far. I actually dont know if we say "While e in " works near the middle of the code. But using the while loop rules, will this code do the same as the for loop one?

safezone = []
hazardflr = givenSteps(roomwidth,step,True)
safetiles = []
m=0
while m < hazardflr:
    safetiles.append((m,step))
    i = 0
    while i < len(safetiles):
        nextSafe = safetiles[i]
        if knownSafe(roomwidth, roomheight, nextSafe[0], nextSafe[1]):
            if trapdoor[nextSafe[0]/roomwidth][nextSafe[0]%roomwidth] is "0":
                if nextSafe[0] not in safezone:
                    safezone.append(nextSafe[0])
                    e=0
                while e in givenSteps(roomwidth,nextSafe[0],True):
                    if knownSafe(roomwidth, roomheight, e, nextSafe[0]):
                        if trapdoor[e/roomwidth][e%roomwidth] is "0" and (e,nextSafe[0]) not in safetiles:
                            safetiles.append((e,nextSafe[0]))
                    e+=1        
        i += 1
    m+=1
return sorted(safezone)

thanks for any advice or help!

No, your code isn't identical.

While they look similar, for item in list and while item in list will do wildly different things .

  • for item in list is a syntactic way of saying for every item in the list - do something with is.
  • while item in list is different - a while loop iterates as long as the condition is true. The condition in this case being item in list . It doesn't update the item each iteration and if you never change what item or list are, it might never terminate. Additionally, if any given item isn't in the list it may terminate prematurely.

If you want to iterate through a list and keep a count, using while is the wrong way to go about it. Use the enumerate() function instead.

enumerate() takes a list, and returns a list of tuples, with each item from the list in order with its index, like so:

for i,m in enumerate(hazardflr):
    safetiles.append((m,step))

This small change means you no longer have to track your indices manually.

If you are iterating through every item in a list in Python - use for that's what it is designed to do.

It depends on exactly what givenSteps returns, but in general, no. for x in foo evaluates foo once and then assigns x to be each element of foo in turn. while x in foo: ... x += 1 , on the other hand, evaluates foo on every iteration and will end early if foo is not a contiguous sequence. For example, if foo = [0, 1, 2, 5, 6] , for will use every value of foo , but while will end after 2, because 3 is not in foo . while will also differ from for if foo contains any non-integral values or values below the starting value.

while aList:
     m= hazardflr.pop()
    # ...

should be roughly equivelent to your other loop

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