简体   繁体   English

为什么循环变量没有在python中更新

[英]Why loop variable is not updated in python

This code is only printing 1 2 4 5..My question is that why p is not updated with the new array at 3rd iteration 这段代码只打印1 2 4 5 ..我的问题是为什么p在第3次迭代时没有用新数组更新

p = [1, 2, [1, 2, 3], 4, 5]
for each in p:
  if type(each) == int:
    print each
  else:
    p = each

Actually to be precise when debugged the code I saw that it actually updating the value of p but each variable is not reinitialised again. 实际上,在调试代码时要确切地说,我看到它实际上更新了p的值但是each变量都没有重新初始化。

Because of if type(each) == int: line. 因为if type(each) == int:行。 Your third element is a list ( [1, 2, 3] ) and not an int, so it doesn't print anything. 你的第三个元素是一个列表( [1, 2, 3] )而不是一个int,因此它不会打印任何内容。

Now what comes to changing the p variable: p is just a name for an object, not the object itself. 现在改变p变量是什么: p只是对象的名称,而不是对象本身。 If you do p = each inside the for loop, it doesn't affect the original object you're looping through, it just changes the name p to a local name, which points to a different object. 如果你在for循环中执行p = each ,它不会影响你循环的原始对象,它只是将名称p更改为本地名称,该名称指向不同的对象。 As soon as that round of the loop ends, your for loop continues to do its business with the original object you were looping through. 一旦循环结束,你的for循环就会继续与你循环的原始对象做生意。

So, notice that p = each doesn't change the existing object (the p you're looping through), it simply creates a new local name p which points to the value of each . 所以,请注意p = each不会改变现有对象(你循环的p ),它只是创建一个新的本地名称p ,它指向each的值。


What you most likely want is something like this: 你最想要的是这样的:

p = [1, 2, [1, 2, 3], 4, 5]
for each in p:
    if isinstance(each, list):
        for x in each:
            print x
    else:
        print each

This then again, this isn't recursive, and you'd need a function for that: 这又一次,这不是递归的,你需要一个函数:

def print_elements(iterable):
    for element in iterable:
        if isinstance(element, list):
            print_elements(element)
        else:
            print element

If you want to unpack the values into one list to use them for something other than printing, you should use something like this: 如果要将值解压缩到一个列表中以将其用于打印以外的其他内容,则应使用以下内容:

def recursive_unpack(iterable):
    for element in iterable:
        if isinstance(element, list):
            yield from recursive_unpack(element)
        else:
            yield element

Why I'm using isinstance() instead of type() : Differences between isinstance() and type() in python 为什么我使用isinstance()而不是type()python中的isinstance()和type()之间的差异

Also, if you want this to apply to all iterables (my last example) and not just lists: In Python, how do I determine if an object is iterable? 此外,如果您希望将此应用于所有迭代(我的上一个示例)而不仅仅是列表: 在Python中,如何确定对象是否可迭代?

the issue is in the else statement, you're basically saying that if the each is not an int (in your case it's a list) then set the list p to the inside list. 问题出在else语句中,你基本上是说如果每个都不是int(在你的情况下它是一个列表),那么将列表p设置为内部列表。

I think that what you're trying to do can be accomplished by something like 我认为你想要做的事情可以用类似的东西来完成

p = [1, 2, [1, 2, 3], 4, 5]
for element in p:
if type(element) == int:
    print element
else:
    for otherElement in element:
        if type(otherElement) == int:
            print otherElement

the else statement in this case goes through the inside list and checks the elements it contains (otherElement) 在这种情况下,else语句遍历内部列表并检查它包含的元素(otherElement)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么变量不在循环外更新? - Why is variable not updated outside of loop? 全局变量未在 while 循环中更新 - Python - Global variable not updated in while loop - Python 带有全局变量的 while 和 for 循环不起作用 Python 已更新 - While and for loop with global variable not working Python updated Python-如果不更改内部变量的值,为什么要更新内部循环的变量? - Python - Why backup variable inside loop is being updated if I don't change it value inside? 在 python 的同一个 for 循环中使用 for 循环末尾的更新变量 - Taking the updated variable at the end of a for loop to be used in the same for loop in python 为什么在每个循环中更新 Totient Function 中的变量“n”? - Why variable `n` in Totient Function updated in every loop? 本地变量未在循环中更新,与Python中的共享内存对象相同 - Local variable not updated in a loop in the same way as shared memory objects in Python Python — 努力了解如何更新 function,因为 while 循环中的变量已更新 - Python — Struggling to understand how to update function, as variable in while loop is updated 为什么 Python 生成器在 for 循环中更新时的行为不同? - Why is the behavior of Python generators different when they're being updated in a for loop? 带有更新值的python for循环 - python for loop with updated values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM