如何在没有 .sort() 的情况下按键对 python 列表进行排序?
How can I sort a python list by key without .sort()?
我想转换这段代码以使其与 Numba 兼容。 Numba 支持的唯一排序方法是 sorted() 但不支持 key arg。我必须在没有其他 lib 导入或可能只是一些 numpy 的情况下手动排序。有人可以给我一个有效的方法来做这种事情吗?谢谢
import random
n = 1000
index = list(range(n))
keys = list(range(n))
random.shuffle(keys)
index.sort(key=lambda x: keys[x])) <= HOW TO CONVERT THIS ?
编辑:
import numpy as np
from numba import jit
@jit(nopython=True)
def fourier_fit_extra(data, harmonic, extra=0):
size = len(data)
x = np.arange(0, size, 1)
m = np.ones((x.shape[0], 2))
m[:, 1] = x
scale = np.empty((2,))
for n in range(0, 2):
norm = np.linalg.norm(m[:, n])
scale[n] = norm
m[:, n] /= norm
lsf = (np.linalg.lstsq(m, data, rcond=-1)[0] / scale)[::-1]
lsd = data - lsf[0] * x
size_lsd = len(lsd)
four = np.zeros(size_lsd, dtype=np.complex128)
for i in range(size_lsd):
sum_f = 0
for n in range(size_lsd):
sum_f += lsd[n] * np.exp(-2j * np.pi * i * n * (1 / size_lsd))
four[i] = sum_f
freq = np.empty(size)
mi = (size - 1) // 2 + 1
freq[:mi] = np.arange(0, mi)
freq[mi:] = np.arange(-(size // 2), 0)
freq *= 1.0 / size
lx = np.arange(0, size + extra)
out = np.zeros(lx.shape)
# IT'S USED TO SORT FOURIER REALS
index = [v for _, v in sorted([(np.absolute(four[v]), v) for v in list(range(size))])][::-1]
for i in index[:1 + harmonic * 2]:
out += (abs(four[i]) / size) * np.cos(2 * np.pi * freq[i] * lx + np.angle(four[i]))
return out + lsf[0] * lx
对于这种特殊类型的输入,您可以通过以下方式实现排序:
for value in index[:]:
index[keys[value]] = value
如果键不是 range(n)
的排列(如您的问题),则创建临时元组,调用 sorted
然后再次从元组中提取值:
result = [value for _, value in sorted(
[(keys[value], value) for value in index]
)]
我想转换这段代码以使其与 Numba 兼容。 Numba 支持的唯一排序方法是 sorted() 但不支持 key arg。我必须在没有其他 lib 导入或可能只是一些 numpy 的情况下手动排序。有人可以给我一个有效的方法来做这种事情吗?谢谢
import random
n = 1000
index = list(range(n))
keys = list(range(n))
random.shuffle(keys)
index.sort(key=lambda x: keys[x])) <= HOW TO CONVERT THIS ?
编辑:
import numpy as np
from numba import jit
@jit(nopython=True)
def fourier_fit_extra(data, harmonic, extra=0):
size = len(data)
x = np.arange(0, size, 1)
m = np.ones((x.shape[0], 2))
m[:, 1] = x
scale = np.empty((2,))
for n in range(0, 2):
norm = np.linalg.norm(m[:, n])
scale[n] = norm
m[:, n] /= norm
lsf = (np.linalg.lstsq(m, data, rcond=-1)[0] / scale)[::-1]
lsd = data - lsf[0] * x
size_lsd = len(lsd)
four = np.zeros(size_lsd, dtype=np.complex128)
for i in range(size_lsd):
sum_f = 0
for n in range(size_lsd):
sum_f += lsd[n] * np.exp(-2j * np.pi * i * n * (1 / size_lsd))
four[i] = sum_f
freq = np.empty(size)
mi = (size - 1) // 2 + 1
freq[:mi] = np.arange(0, mi)
freq[mi:] = np.arange(-(size // 2), 0)
freq *= 1.0 / size
lx = np.arange(0, size + extra)
out = np.zeros(lx.shape)
# IT'S USED TO SORT FOURIER REALS
index = [v for _, v in sorted([(np.absolute(four[v]), v) for v in list(range(size))])][::-1]
for i in index[:1 + harmonic * 2]:
out += (abs(four[i]) / size) * np.cos(2 * np.pi * freq[i] * lx + np.angle(four[i]))
return out + lsf[0] * lx
对于这种特殊类型的输入,您可以通过以下方式实现排序:
for value in index[:]:
index[keys[value]] = value
如果键不是 range(n)
的排列(如您的问题),则创建临时元组,调用 sorted
然后再次从元组中提取值:
result = [value for _, value in sorted(
[(keys[value], value) for value in index]
)]