Python 官方文档:入门教程 => 点击学习
******python 不需要交换函数swap(),如果要交换a,b的话,只需要使用如下语句: a,b = b,a 即可(因为:Python以引用方式管理对象,你可以交换引用,但通常不能交换内存中的对象值。当然你也不需要这样做。)
******python 不需要交换函数swap(),如果要交换a,b的话,只需要使用如下语句:
a,b = b,a
即可(因为:Python以引用方式管理对象,你可以交换引用,但通常不能交换内存中的对象值。当然你也不需要这样做。)
在python中应该这样做:
a = 1
b = 2
def swap(t1, t2):
return t2, t1
a,b = swap(a, b) # After this point, a == 2 and b == 1
But there is not way (other than abusing globals or the module
namespace) to do it like this:
不过下面这段代码不可能像我们希望的那样工作(全局命名空间和局部命名空间是隔离的):
a = 1
b = 2
def swap(t1, t2):
t2, t1 = t1, t2
return
swap(a, b)
# After this point, a == 1 and b == 2. The calling namespace is
# not changed.
--结束END--
本文标题: Python 中怎么写 swap()交换
本文链接: https://www.lsjlt.com/news/192040.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0