python3数据补齐之中位数
常规:
'''median''' def getmedian(numlist): numlist.sort() half = len(numlist) // 2 return (numlist[half] + numlist[~half]) / 2 '''the list fill with median''' def fillnum(numlist): length = len(numlist) if 0 < length < 10: median = round(getmedian(numlist), 2) numlist.reverse() i = 0 while i < 10 - length: numlist.append(median) i = i + 1 numlist.reverse() return numlist else: return numlist
numpy库:
import numpy as np
np.pad(ns, (10 - len(ns), 0), 'median')
提示:Node.js中可用fill函数