Python交换值在更改交换顺序时给出了不同的结果

发布于 2025-02-06 01:36:16 字数 540 浏览 4 评论 0原文

输入:
[1,2,3,4]

预期输出:

[2,1,3,4]

给出了预期结果

a = [1,2,3,4]
a[a[0]], a[0] = a[0],a[a[0]]
#Output -> [2, 1, 3, 4]

以下代码在更改交换后, 。订单给出了不正确的输出

a = [1,2,3,4]
a[0],a[a[0]] = a[a[0]],a[0]
# Output -> [2, 2, 1, 4]

,我刚刚切换了作业的顺序。为什么会有所作为?

编辑: 那么,为什么下面的两个案例给出相同的输出:

a = 1 b= 2 
a,b = b,a #Case1
b,a = a,b #Case2
print(a,b) #=> prints 2,1 for both

如果问题不够描述,请让我知道我会添加更多评论。 我试图使其尽可能简单。 谢谢

Input:
[1,2,3,4]

Expected Output:

[2, 1, 3, 4]

The below code gives the expected result

a = [1,2,3,4]
a[a[0]], a[0] = a[0],a[a[0]]
#Output -> [2, 1, 3, 4]

The below code ie, after changing swapping order gives the incorrect output

a = [1,2,3,4]
a[0],a[a[0]] = a[a[0]],a[0]
# Output -> [2, 2, 1, 4]

I just switched the order of the assignments. Why does that make a difference?

Edit:
Then why do the two cases below give the same output:

a = 1 b= 2 
a,b = b,a #Case1
b,a = a,b #Case2
print(a,b) #=> prints 2,1 for both

If the question is not descriptive enough please let me know I will add more comments.
I have tried to make it as simple as possible.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笏见 2025-02-13 01:36:16

这很重要,因为您基本上执行两个单独的作业。这些作业之一必须首先执行。因此,如果您首先分配a [0],则a [a [0]]已经可以与a [0] 确定索引。如果您首先分配a [a [0]],则该索引的行为按预期行为,并且之后只有a [0]更改的值。

It matters because you basically perform two separate assignments. One of those assignements has to be performed first. So if you assign a[0] first, then a[a[0]] will already work with the new value of a[0] to determine the index. If you first assign a[a[0]] then the index behaves as expected and only afterwards the value of a[0] changes.

时间你老了 2025-02-13 01:36:16

Python首先评估整个右侧,然后从左到右分配到左侧。在第一种情况下:

tmp1=a[0]      # that is tmp1=1
tmp2=a[a[0]]   # that is tmp2=a[1] that is tmp2=2
a[a[0]]=tmp1   # that is a[1]=1
a[0]=tmp2      # that is a[0]=2

在第二种情况下:

tmp1=a[a[0]]   # that is tmp1=a[1], that is tmp1=2
tmp2=a[0]      # that is tmp2=1
a[0]=tmp1      # that is a[0]=2
a[a[0]]=tmp2   # that is a[2]=1

请参见 https:https:// docss:// docs .python.org/3/reference/expressions.html#evaluation-order

Python evaluates first the whole right-hand side, then assigns to the left hand side from left to right. In the first case:

tmp1=a[0]      # that is tmp1=1
tmp2=a[a[0]]   # that is tmp2=a[1] that is tmp2=2
a[a[0]]=tmp1   # that is a[1]=1
a[0]=tmp2      # that is a[0]=2

In the second case:

tmp1=a[a[0]]   # that is tmp1=a[1], that is tmp1=2
tmp2=a[0]      # that is tmp2=1
a[0]=tmp1      # that is a[0]=2
a[a[0]]=tmp2   # that is a[2]=1

See https://docs.python.org/3/reference/expressions.html#evaluation-order

暮年 2025-02-13 01:36:16

两者都会返回不同的结果,您的代码含义上述

a = [1,2,3,4]
a[a[0]], a[0] = a[0],a[a[0]]
# code above access a[with index a[0]] = 1
# simply means that a[1], a[0] = a[0], a[1]
# and equal with [2, 1, 3, 4]
#Output -> [2, 1, 3, 4]

代码返回您的预期结果,但是不正确的结果呢?

a = [1,2,3,4]
a[0],a[a[0]] = a[a[0]],a[0]
# this means a[0], a[1] = a[1], a[0]
# Output -> [2, 2, 1, 4]

解释:
您定义第一个a [0] = a [a [0]]a [0] = a [1](当前值为2)或在简单的单词中a [0] = 2
接下来,您调用a [a [0]] = a [0]a [2] = a [0](您已经将A [0]定义为2)< /code>或简单词a [2] = 2
这为您的结果提供了[2,2,1,4]的结果,

用于交换值时的其他情况

a = 1 b= 2 
a,b = b,a #Case1
b,a = a,b #Case2
print(a,b) #=> prints 2,1 for both

,使用变量的简单案例,并且像常见的交换案例一样工作,但是您使用列表的示例不正确,这更棘手,就像我已经很棘手以前解释过

Both will return different result, your code means like this

a = [1,2,3,4]
a[a[0]], a[0] = a[0],a[a[0]]
# code above access a[with index a[0]] = 1
# simply means that a[1], a[0] = a[0], a[1]
# and equal with [2, 1, 3, 4]
#Output -> [2, 1, 3, 4]

Above code return what your expected result, but how about the incorrect one?

a = [1,2,3,4]
a[0],a[a[0]] = a[a[0]],a[0]
# this means a[0], a[1] = a[1], a[0]
# Output -> [2, 2, 1, 4]

Explanation:
You define the first a[0] = a[a[0]] which equal with a[0] = a[1] (which current value is 2) or in simple word a[0] = 2.
Next, you call a[a[0]] = a[0] which equal with a[2] = a[0] (you already define a[0] as 2) or in simple word a[2] = 2
And this give you the result as [2, 2, 1, 4]

For the additional case in swapping value

a = 1 b= 2 
a,b = b,a #Case1
b,a = a,b #Case2
print(a,b) #=> prints 2,1 for both

This's simple case using variable, and work like common swapping case, but your incorrect sample using list, which are more tricky as I already explained before

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文