python a,b = b,一个实现?它与C ++交换函数有何不同? [英] python a,b = b,a implementation? How is it different from C++ swap function?

查看:138
本文介绍了python a,b = b,一个实现?它与C ++交换函数有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想尝试以下版本的python版本时遇到了这个问题:
https://leetcode.com/problems/first-missing-positive/discuss/17071/My-short- c ++-solution-O(1)-space-and-O(n)-时间

I met this problem when I want to try a python version of: https://leetcode.com/problems/first-missing-positive/discuss/17071/My-short-c++-solution-O(1)-space-and-O(n)-time

我不确定为什么 a [0],a [a [0]] = a [a [0]],a [0] 这个不做交换吗?

I am not sure why a[0], a[a[0]] = a[a[0]], a[0] this one does not do the swap?

>>> nums
[2, 1, 0]
>>> a = [2,1,0]
>>> a[0], a[a[0]] = a[a[0]], a[0]
>>> a
[2, 1, 0]
>>> a[0]
2
>>> a[0],a[2] = a[2], a[0]
>>> a
[0, 1, 2]

我的猜测是a的实现, b = b,语法类似于:

My guess is that the implementation of a, b = b, a syntax is something like:

tmp = a[0] (tmp = 2)
a[0]  = a[a[0]] (a[0] = a[2] = 0)
a[a[0]] = tmp (a[a[0]] = a[0] = tmp = 2)

然后我检查了C ++中swap函数的实现。我对C ++一无所知,但是看起来这个想法是一样的

http://www.cplusplus.com/reference/algorithm/swap/

Then I checked the implementation of swap function in C++. I know nothing about C++, but it looks like the idea is the same : http://www.cplusplus.com/reference/algorithm/swap/

The behavior of these function templates is equivalent to:
template <class T> void swap (T& a, T& b)
{
  T c(std::move(a)); a=std::move(b); b=std::move(c);
}
template <class T, size_t N> void swap (T (&a)[N], T (&b)[N])
{
  for (size_t i = 0; i<N; ++i) swap (a[i],b[i]);
}

我们有c = a,然后a = b和b = a
那么为什么C ++交换函数没有这个问题呢?
以及如何以pythonic方式编写这种交换函数?

we have c = a, then a = b and b = a So why C++ swap function does not have this problem? And how to write this kind of swap function in a pythonic way?

推荐答案

这种行为的确是与Python评估类型的表达式的方式有关

This kind of the behaviour is indeed related to the way Python evaluates the expression of the type

a,b=b,a

事实上,Python所做的是首先通过创建元组(准备右侧的值)( b,a)。然后将此元组解压缩并以相反的顺序分配给变量。

In fact, what Python does is first it "prepares" the values of the right side by creating a tuple (b,a). Then this tuple is unpacked and assigned to the variables in the reverse order.

请注意,尽管Python使用引用来对象化对象, 反对如果变量名称引用的是不可变类型的值,则可能会更改。并非 mutable 类型(通过Python常见问题解答示例)。

It is important to note that although Python uses references to objects the objects the variable names refer to may change if they refer to values of immutable type. It is not so with mutable types (illustrated by example in Python FAQ).

您使用的可变类型(列表)的示例:

To break down the example with mutable types (lists) that you used:

a = [2,1,0]    
a[0], a[a[0]] = a[a[0]], a[0]




  1. a [a [0]] a [0] 中取值 a (值 0 2 ) c>)。

  2. a [0] 2 ,因此元组创建的是(0,2)

  3. 元组(0,2)已解压缩,并且 0 替换了列表中的 2 (第0个元素)。

  4. 现在, a [a [0]] 可以理解为:取列表 a 的第0个元素(即当前 0 ),然后将该位置的列表中的值替换为元组拆包中的 2 (现在 0 被替换为 2 -使操作看起来像对列表没有任何作用。)

  1. a[a[0]] takes the value from the a[0] element (equal to 2) of the list a (value 0).
  2. a[0] is 2 hence the tuple created is (0,2)
  3. Tuple (0,2) is unpacked and 0 replaces 2 in the list (0th element).
  4. Now, a[a[0]] can be read as: take 0th element of list a (which is currently 0) and then replace the value in the list at that place with 2 from tuple unpacking (now 0 is replaced by 2 - which make the operation look like it does nothing to the list).

冯·奥克 的答案更改顺序很有帮助,因为上述第4点的步骤不能代替

As suggested in the answer from von Oak changing the order helps because the step from the point 4. above does not replace the value again.

我建议您参考通过分配传递理解函数和参数传递的答案。

I suggest you refer to passing by assignment answer to understand functions and parameter passing.

这篇关于python a,b = b,一个实现?它与C ++交换函数有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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