众所周知,pythonic 的方式来交换两个项目的值,a并且b是
a, b = b, a
Run Code Online (Sandbox Code Playgroud)
它应该相当于
b, a = a, b
Run Code Online (Sandbox Code Playgroud)
但是,今天在写代码的时候,无意中发现下面的两个swap给出了不同的结果:
nums = [1, 2, 4, 3]
i = 2
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
print(nums)
# [1, 2, 4, 3]
nums = [1, 2, 4, 3]
i = 2
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
print(nums)
# [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
这对我来说令人难以置信。有人可以向我解释这里发生了什么吗?我认为在 Python 交换中,这两个任务同时且独立地发生。
In [55]: a = 5
In [56]: b = 6
In [57]: (a, b) = (b, a)
In [58]: a
Out[58]: 6
In [59]: b
Out[59]: 5
Run Code Online (Sandbox Code Playgroud)
如何交换a和b的值在内部工作?它肯定不使用临时变量.
我喜欢python处理变量交换的方式:
a, b, = b, a
我想使用这个功能来交换数组之间的值,不仅一次一个,而且还有一些(不使用临时变量).这不符合我的预期(我希望第三维的两个条目都可以互换):
import numpy as np
a = np.random.randint(0, 10, (2, 3,3))
b = np.random.randint(0, 10, (2, 5,5))
# display before
a[:,0, 0]
b[:,0,0]
a[:,0,0], b[:, 0, 0] = b[:, 0, 0], a[:,0,0] #swap
# display after
a[:,0, 0]
b[:,0,0]
Run Code Online (Sandbox Code Playgroud)
有没有人有想法?当然我总是可以引入一个额外的变量,但我想知道是否有更优雅的方式来做到这一点.
有没有比以下代码更有效的东西来交换numpy 1D数组的两个值?
input_seq = arange(64)
ix1 = randint(len(input_seq))
ixs2 = randint(len(input_seq))
temp = input_seq[ix2]
input_seq[ix2] = input_seq[ix1]
input_seq[ix1] = temp
Run Code Online (Sandbox Code Playgroud) 码:
x="salil"
y="Ajay"
y,x=x,y
print x +" "+ y
Run Code Online (Sandbox Code Playgroud)
输出:
Ajay salil
Run Code Online (Sandbox Code Playgroud)
对此有何合理解释?
过去几天我一直在努力更好地理解计算复杂性以及如何改进Python代码.为此,我尝试了不同的函数来计算Fibonacci数,比较如果我进行小的更改,脚本运行的时间.
我正在使用列表计算斐波那契数字,从列表中添加元素-2和-1的总和.
我很困惑地发现,如果我在循环中添加.pop(),删除列表中不需要的元素,我的脚本运行得更快.我不明白为什么会这样.循环中的每一步计算机都会做一件事.所以我未经训练的直觉表明这会增加计算时间.当列表很长时,"查找"列表的最后一个元素会慢得多吗?
这是我的代码:
import time
import numpy as np
def fib_stack1(n):
""" Original function """
assert type(n) is int, 'Expected an integer as input.'
if n < 2:
return n
else:
stack = [0, 1]
for i in range(n-1):
stack.append(stack[-1] + stack[-2])
return stack[-1]
def fib_stack2(n):
""" Modified function """
assert type(n) is int, 'Expected an integer as input.'
if n < 2:
return n
else:
stack = [0, 1]
for i in range(n-1):
stack.append(stack[-1] + stack[-2])
### CHANGE …Run Code Online (Sandbox Code Playgroud) 如果我执行以下操作,则在python列表中交换元素的时间复杂度是多少?
>>> a = [1, 2, 3, 4, 5]
>>> a[1], a[3] = a[3], a[1] # Pythonic Swap --> x, y = y, x
>>> print(a)
[1, 4, 3, 2, 5]
Run Code Online (Sandbox Code Playgroud)
问题1:交换步骤的时间复杂度是多少?python内部做了什么.
>>> a = [1, 2, 3, 4, 5]
>>> temp1, temp2 = a[1], a[3]
>>> del a[1] # a = [1, 3, 4, 5]
>>> a.insert(1, temp2) # a = [1, 4, 3, 4, 5]
>>> del a[3] # a = [1, 4, …Run Code Online (Sandbox Code Playgroud) 我bubbleSort function在 python 中有这个可以完美运行。
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
Run Code Online (Sandbox Code Playgroud)
我是 python 新手,无法理解 if 语句下面的代码。怎样arr[j], arr[j+1] = arr[j], arr[j+1]运作?
下面的代码在 Python 中是如何工作的:
a = input()
b = input()
a, b = b, a # STATEMENT 1
print(a, b)
Run Code Online (Sandbox Code Playgroud)
语句 1 是否在 Python 堆内存空间中创建第三个变量来交换两个数字,或者是否使用某种算法来进行交换?
当我运行此脚本时,它会在此语句中返回错误 no1:=(no1+no2)-(no2:=no1);
declare
no1 number(3):=31;
no2 number(3):=34;
begin
dbms_output.put_line('Before swap');
dbms_output.put_line('No1 : '||no1||' No2 : '||no2 );
-- no1:=(no1+no2)-(no2:=no1); generate error
dbms_output.put_line('After swap');
dbms_output.put_line('No1 : '||no1||' No2 : '||no2 );
end;
Run Code Online (Sandbox Code Playgroud)