Python多重赋值问题(列表) [英] Python multiple assignment issue (list)

查看:46
本文介绍了Python多重赋值问题(列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预><代码>>>>我 = 1>>>A = [3,4,-1,1]>>>A[A[i] - 1], A[i] = A[i], A[A[i] - 1]>>>一种[3, 1, -1, 4]>>>A = [3,4,-1,1]>>>A[i], A[A[i] - 1] = A[A[i] - 1], A[i]>>>一种[4, 1, -1, 1]

当我为列表的多个变量赋值时有疑问.就像上面的例子,赋值

A[A[i] - 1], A[i] = A[i], A[A[i] - 1]

与作业不同

A[i], A[A[i] - 1] = A[A[i] - 1], A[i]

我真的很困惑Python中的内部计算顺序.为什么结果不同?在一行中完成这种多重分配的最佳方法是什么?

解决方案

Per 文档:

<块引用>

Python 从左到右计算表达式.请注意,虽然评估一个赋值,右侧在评估之前左侧.

有关详细信息,请参阅本部分.此处有一个利用这种行为的脑筋急转弯的好例子.

这意味着 = 的右侧首先被评估,从左到右,然后分配到左侧,从左到右.必要时,括号是由内而外评估的.逐步分解,这是第一个示例:

i = 1A = [3, 4, -1, 1]A[A[i] - 1], A[i] = A[i], A[A[i] - 1]= A[1], A[A[i] - 1]= 4, A[A[i] - 1]= 4, A[A[1] - 1]= 4, A[4 - 1]= 4, A[3]= 4, 1A[A[i] - 1], A[i] = 4, 1A[A[1] - 1], A[i] = 4, 1A[4 - 1], A[i] = 4, 1A[3], A[i] = 4, 1 # A 变成 [3, 4, -1, 4]A[i] = 1A[1] = 1 # A 变成 [3, 1, -1, 4]

这是第二个:

i = 1A = [3, 4, -1, 1]A[i], A[A[i] - 1] = A[A[i] - 1], A[i]= A[A[1] - 1], A[i]= A[4 - 1], A[i]= A[3], A[i]= 1, A[i]= 1, A[1]= 1, 4A[i], A[A[i] - 1] = 1, 4A[1], A[A[i] - 1] = 1, 4 # A 变成 [3, 1, -1, 1]A[A[1] - 1] = 4A[1 - 1] = 4A[0] = 4 # A 变成 [4, 1, -1, 1]

对左侧目标的赋值改变了 A 的内容,从而改变了右侧目标的索引.4 被分配给 A[3]A[0],具体取决于 A[1]<的值/code>(从 4 变为 1)在计算索引时.

在一行中完成这种多重分配的最佳方法是什么?" - 我会尽力避免它.我想不出有什么情况需要像这样分配给移动目标.

>>> i = 1
>>> A = [3,4,-1,1]
>>> A[A[i] - 1], A[i]  =  A[i], A[A[i] - 1]
>>> A
[3, 1, -1, 4]
>>> A = [3,4,-1,1]
>>> A[i], A[A[i] - 1] = A[A[i] - 1], A[i]
>>> A
[4, 1, -1, 1]

I have question when I do assignment for multiple variables for a list. Like the example above, the assignment

A[A[i] - 1], A[i]  =  A[i], A[A[i] - 1]

is different from the assignment

A[i], A[A[i] - 1] = A[A[i] - 1], A[i]

I am really confused the inside calculation order in Python. Why the results are different? What's the best way to do this kind of multiple assignment in one line?

解决方案

Per the documentation:

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

For more detail, see this section. There is a good example of a brain-teaser exploiting this behaviour here.

This means that the right-hand side of the = is evaluated first, left-to-right, then the assignment is made to the left-hand side, left-to-right. Necessarily, the brackets are evaluated inside-out. Breaking that down stepwise, here's the first example:

i = 1
A = [3, 4, -1, 1]

A[A[i] - 1], A[i] = A[i], A[A[i] - 1]
                  = A[1], A[A[i] - 1]
                  = 4, A[A[i] - 1]
                  = 4, A[A[1] - 1]
                  = 4, A[4 - 1]
                  = 4, A[3]
                  = 4, 1
A[A[i] - 1], A[i] = 4, 1
A[A[1] - 1], A[i] = 4, 1
A[4 - 1], A[i] = 4, 1
A[3], A[i] = 4, 1  # A becomes [3, 4, -1, 4]
A[i] = 1
A[1] = 1  # A becomes [3, 1, -1, 4]

And here's the second:

i = 1
A = [3, 4, -1, 1]

A[i], A[A[i] - 1] = A[A[i] - 1], A[i]
                  = A[A[1] - 1], A[i]
                  = A[4 - 1], A[i]
                  = A[3], A[i]
                  = 1, A[i]
                  = 1, A[1]
                  = 1, 4
A[i], A[A[i] - 1] = 1, 4
A[1], A[A[i] - 1] = 1, 4  # A becomes [3, 1, -1, 1]
A[A[1] - 1] = 4
A[1 - 1] = 4
A[0] = 4  # A becomes [4, 1, -1, 1]

The assignment to the left-hand target on the left-hand side alters the content of A, which changes the indexing in the right-hand target. The 4 is assigned to either A[3] or A[0], depending on the value of A[1] (which changes from 4 to 1) when the index is calculated.

"What's the best way to do this kind of multiple assignment in one line?" - I'd do my very best to avoid it. I can't think of any situation where it would be necessary to assign to moving targets like this.

这篇关于Python多重赋值问题(列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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