python - 交换 numpy 数组中的两个值。

标签 python numpy scipy swap

有没有比以下代码更有效地交换 numpy 一维数组的两个值的方法?

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

最佳答案

我看到您正在使用 numpy 数组。在这种情况下,您也可以这样做:

input_seq[[ix1, ix2]] = input_seq[[ix2, ix1]]

关于python - 交换 numpy 数组中的两个值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22847410/

相关文章:

python - 脚本之间的全局变量

python - 如何使用另一个数组中的值更新 numpy 数组的所有值

python - interpolate.splev 错误 : 'Lengths of the first three arguments (x,y,w) must be equal'

python等效数学方程给出不同的结果

python - boost python返回包含自定义类型的元组

php - GAE : Push Task Queues - How to push tasks to specific Queues? - 使用 GAE-PHP

python - SciPy/NumPy : Normalize a csr_matrix

python - 你如何在 Numpy 中找到 IQR?

python - 在 Python 中找到带边界的函数的最大值

python - 带 ILU 预处理器的一般最小残差 (GMRES)