【问题标题】:For Loop to While Loop using IN for while loopsFor 循环到 While 循环 使用 IN for while 循环
【发布时间】:2014-07-11 03:23:47
【问题描述】:

我是 Python 2.7 的新手,所以我有几个关于使用 for 循环到 while 循环的问题。

例如:我正在写这个定义

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)

我正在尝试将所有 for 循环转换为 while 循环,所以这是我目前所写的。我实际上不知道我们是否在代码中间说“While e in”。但是使用while循环规则,这段代码会和for循环一样吗?

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)

感谢您的任何建议或帮助!

【问题讨论】:

  • “我正在尝试将所有 for 循环转换为 while 循环”——为什么?
  • @MattDMo:实际上是这样; in 只是成员资格测试操作员。
  • @MattDMo while e in ... 有什么问题?
  • @SteveZrg 通常在 Python 中,编码人员应该学习相反的方向,将无效的 while 循环按索引访问项目以使用 for 循环。但是可能有像这里这样的原因,当迭代在处理过程中被修改时(参见safetiles.append((e, nextSafe[0]))无论如何,只有当你有充分的理由时,你才应该执行while循环。

标签: python python-2.7 if-statement for-loop while-loop


【解决方案1】:

不,你的代码不一样。

虽然它们看起来很相似,但 for item in listwhile item in list 会做截然不同的事情

  • for item in list 是对列表中每个项目的一种语法表达方式 - 用 is 做某事。
  • while item in list 不同 - 只要条件为真,while 循环就会迭代。这种情况下的条件是item in list。它不会在每次迭代时更新项目,如果您从不更改 itemlist 的内容,它可能永远不会终止。此外,如果任何给定项目不在列表中,它可能会提前终止。

如果你想遍历一个列表并保持计数,使用while 是错误的方法。请改用enumerate() 函数。

enumerate() 接受一个列表,并返回一个元组列表,列表中的每个项目与其索引按顺序排列,如下所示:

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

这个小改动意味着您不再需要手动跟踪索引。

如果您在 Python 中遍历列表中的每个项 - 使用for,这就是它的设计目的。

【讨论】:

  • 那么,我如何使用枚举重写这段代码?我是否只是用 for i,m in enumerate(hazardflr) 替换 while 循环:
【解决方案2】:

这完全取决于givenSteps 返回的内容,但总的来说,不会。 for x in foo 计算一次foo,然后依次将x 分配为foo 的每个元素。另一方面,while x in foo: ... x += 1 在每次迭代时评估 foo,如果 foo 不是连续序列,则将提前结束。例如,如果foo = [0, 1, 2, 5, 6]for 将使用foo 的每个值,但while 将在2 之后结束,因为3 不在foo 中。如果foo 包含任何非整数值或低于起始值的值,while 也将不同于 for

【讨论】:

    【解决方案3】:
    while aList:
         m= hazardflr.pop()
        # ...
    

    应该与您的其他循环大致等效

    【讨论】:

    • 除非这有破坏原始列表的副作用,这可能是不想要的。
    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    相关资源
    最近更新 更多