Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在根据另一个列表缩放的值范围内列出_Python_Python 3.x_List_Numpy - Fatal编程技术网

Python 在根据另一个列表缩放的值范围内列出

Python 在根据另一个列表缩放的值范围内列出,python,python-3.x,list,numpy,Python,Python 3.x,List,Numpy,我有一张单子- x=[27,28,29,28,31,32,29,30,35,36,33,37,39] 和范围-(02000) 如何根据列表创建具有此范围内值的新列表?27是0,39是2000 x = [27, 28, 29, 28, 31, 32, 29, 30, 35, 36, 33, 37, 39] r = (0, 2000) lower = min(x) upper = max(x) print ([(i - lower) * ((r[1] - r[0]) / (upper - low

我有一张单子-
x=[27,28,29,28,31,32,29,30,35,36,33,37,39]
和范围-(02000) 如何根据列表创建具有此范围内值的新列表?27是0,39是2000

x = [27, 28, 29, 28, 31, 32, 29, 30, 35, 36, 33, 37, 39]
r = (0, 2000)

lower = min(x)
upper = max(x)

print ([(i - lower) * ((r[1] - r[0]) / (upper - lower)) + r[0] for i in x])
如果需要整数值而不是浮点数,请在列表中插入
int()
round()

编辑:我更改了代码,以提供一个更通用的解决方案,该解决方案也适用于(0,2000)之外的其他范围

如果需要整数值而不是浮点数,请在列表中插入
int()
round()


编辑:我更改了代码,以提供一个更通用的解决方案,该解决方案也适用于(0,2000)之外的其他范围。

您标记了numpy,下面是使用它的答案:

>>> import numpy as np
>>> x = np.array([27, 28, 29, 28, 31, 32, 29, 30, 35, 36, 33, 37, 39])
>>> 2000 * (x - x.min()) / (x.max() - x.min())
array([   0.        ,  166.66666667,  333.33333333,  166.66666667,
        666.66666667,  833.33333333,  333.33333333,  500.        ,
       1333.33333333, 1500.        , 1000.        , 1666.66666667,
       2000.        ])

您标记了numpy,下面是使用它的答案:

>>> import numpy as np
>>> x = np.array([27, 28, 29, 28, 31, 32, 29, 30, 35, 36, 33, 37, 39])
>>> 2000 * (x - x.min()) / (x.max() - x.min())
array([   0.        ,  166.66666667,  333.33333333,  166.66666667,
        666.66666667,  833.33333333,  333.33333333,  500.        ,
       1333.33333333, 1500.        , 1000.        , 1666.66666667,
       2000.        ])

您可以使用
列表理解
min
len

>[(i-min(x))*2000/(len(x)-1)表示x中的i]
[0.0, 166.66666666666666, 333.3333333333333, 166.66666666666666, 666.6666666666666, 833.3333333333334, 333.3333333333333, 500.0, 1333.3333333333333, 1500.0, 1000.0, 1666.6666666666667, 2000.0]

您可以使用
列表理解
min
len

>[(i-min(x))*2000/(len(x)-1)表示x中的i]
[0.0, 166.66666666666666, 333.3333333333333, 166.66666666666666, 666.6666666666666, 833.3333333333334, 333.3333333333333, 500.0, 1333.3333333333333, 1500.0, 1000.0, 1666.6666666666667, 2000.0]
这里有一个解决方案:

>>> x = [27, 28, 29, 28, 31, 32, 29, 30, 35, 36, 33, 37, 39]
>>> y = [i* 2000/(len(x)-1) for i in range(len(x))]
>>> print(y)
>>> [0.0, 166.66666666666666, 333.3333333333333, 500.0, 666.6666666666666, 833.3333333333334, 1000.0, 1166.6666666666667, 1333.3333333333333, 1500.0, 1666.6666666666667, 1833.3333333333333, 2000.0]
这里有一个解决方案:

>>> x = [27, 28, 29, 28, 31, 32, 29, 30, 35, 36, 33, 37, 39]
>>> y = [i* 2000/(len(x)-1) for i in range(len(x))]
>>> print(y)
>>> [0.0, 166.66666666666666, 333.3333333333333, 500.0, 666.6666666666666, 833.3333333333334, 1000.0, 1166.6666666666667, 1333.3333333333333, 1500.0, 1666.6666666666667, 1833.3333333333333, 2000.0]

如果我有像
[[27,28,29,28],[28,30,32,29],[31,32,28,33]]
这样的numpy数组,我希望保留相同的结构,但其中的值range@Prak上面的代码也适用于该输入。试试看!这个怎么样
np.interp(x,(x.min(),x.max(),(02000))
@Prak这是正确的答案。好发现!如果我有像
[[27,28,29,28],[28,30,32,29],[31,32,28,33]]
这样的numpy数组,我希望保留相同的结构,但其中的值range@Prak上面的代码也适用于该输入。试试看!这个怎么样
np.interp(x,(x.min(),x.max(),(02000))
@Prak这是正确的答案。好发现!