![](/img/trans.png)
[英]How to sort a list of N elements and then replace missing values between 0 to N with -1
[英]List between 2 ranges and n elements in python
我如何創建一個數字列表,它將是一個系列? 例如 function(0, 2, 5) from 0 to 2 with 5 elements --> [0, 0.5, 1, 1.5, 2] python中有什么函數可以做到嗎?
numpy
做你想做的事:
>>> import numpy as np
>>> np.linspace(0, 2, 5)
array([0. , 0.5, 1. , 1.5, 2. ])
如果你真的需要它是一個列表,那么你可以這樣做:
>>> list(np.linspace(0, 2, 5))
[0.0, 0.5, 1.0, 1.5, 2.0]
這是一個計算增量並創建數組的簡單函數。
def n_spaced_range(x_start, x_end, n_elements):
d = (x_end - x_start) / (n_elements-1)
return [x_start + i*d for i in range(n_elements)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.