for循环中的Python变量赋值 [英] Python Variable assignment in a for loop

查看:61
本文介绍了for循环中的Python变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 Python 中,常规的 C++ 样式变量赋值被对东西的引用替换,即

I understand that in Python regular c++ style variable assignment is replaced by references to stuff ie

a=[1,2,3]
b=a
a.append(4)
print(b)       #gives [1,2,3,4]
print(a)       #gives [1,2,3,4]

但我仍然很困惑为什么基本类型的类似情况,例如.整数的工作方式不同?

but I'm still confused why an analogous situation with basic types eg. integers works differently?

a=1
b=a
a+=1
print(b)          # gives 1
print(a)          # gives 2

等等,当我们考虑循环时,它会变得更加混乱!

But wait, it gets even more confusing when we consider loops!

li=[1,2,3]
for x in li:
    x+=1
print(li)     #gives [1,2,3]

这是我的预期,但如果我们这样做会发生什么:

Which is what I expected, but what happens if we do:

a,b,c=1,2,3
li=[a,b,c]
for x in li:
    x+=1
print(li)        #gives [1,2,3]

也许我的问题应该是如何遍历整数列表并在没有 map() 的情况下更改它们,因为我需要在那里使用 if 语句.我唯一不能使用的东西

Maybe my question should be how to loop over a list of integers and change them without map() as i need a if statement in there. The only thing I can come up short of using

for x in range(len(li)):
    Do stuff to li[x]

将整数打包在一个元素列表中.但一定有更好的方法.

is packaging the integers in one element list. But there must be a better way.

推荐答案

a=[1,2,3]
b=a
a.append(4)
print(b)       #[1,2,3,4]
print(a)       #[1,2,3,4]

在这里您正在修改列表.列表内容更改,但列表标识保持不变.

Here you are modifying the list. The list content changes, but the list identity remains.

a=1
b=a
a+=1

然而,这是重新分配.您将不同的对象分配给 a.

This, however, is a reassignment. You assign a different object to a.

请注意,如果您在第一个示例中执行 a += [4],您会看到相同的结果.这是因为 a += somethinga = a.__iadd__(something) 相同,但回退到 a = a.__add__(东西) 如果 __iadd__() 不存在.

Note that if you did a += [4] in the 1st example, you would have seen the same result. This comes from the fact that a += something is the same as a = a.__iadd__(something), with a fallback to a = a.__add__(something) if __iadd__() doesn't exist.

区别在于 __iadd__() 尝试通过修改它处理的对象并返回它来就地"完成它的工作.所以 a 指的和之前一样.这仅适用于可变对象,例如列表.

The difference is that __iadd__() tries to do its job "inplace", by modifying the object it works on and returning it. So a refers to the same as before. This only works with mutable objects such as lists.

在不可变对象上,例如 ints __add__() 被调用.它返回一个不同的对象,这导致 a 比以前指向另一个对象.没有其他选择,因为整数是不可变的.

On immutable objects such as ints __add__() is called. It returns a different object, which leads to a pointing to another object than before. There is no other choice, as ints are immutable.

a,b,c=1,2,3
li=[a,b,c]
for x in li:
    x+=1
print(li)        #[1,2,3]

这里x += 1x = x + 1 的意思相同.它会更改 x 所指的位置,但不会更改列表内容.

Here x += 1 means the same as x = x + 1. It changes where x refers to, but not the list contents.

也许我的问题应该是如何遍历整数列表并在没有 >map() 的情况下更改它们,因为我需要在那里使用 if 语句.

Maybe my question should be how to loop over a list of integers and change them without >map() as i need a if statement in there.

for i, x in enumerate(li):
    li[i] = x + 1

将旧值 + 1 分配给每个列表位置.

assigns to every list position the old value + 1.

这篇关于for循环中的Python变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆