Python 类快速排序方法找到第定n小数的方法,python小数,python 用类快排的
Python 类快速排序方法找到第定n小数的方法,python小数,python 用类快排的
python 用类快排的方法找寻"第n小"的数,Python 类快速排序方法找到第定n小数的方法。
(n)快速排序风格基于排序算法查找数据。
有用的寻找中位数,百分位数、四分位数和十分位数。
相当于数据[n]当已经排序的数据。
#coding=utf-8import randomdef select(data, n): "Find the nth rank ordered element (the least value has rank 0)." data = list(data) if not 0 <= n < len(data): raise ValueError('not enough elements for the given rank') while True: pivot = random.choice(data) pcount = 0 under, over = [], [] uappend, oappend = under.append, over.append#www.iplaypy.com for elem in data: if elem < pivot: uappend(elem) elif elem > pivot: oappend(elem) else: pcount += 1 if n < len(under): data = under elif n < len(under) + pcount: return pivot else: data = over n -= len(under) + pcount
编橙之家文章,
相关内容
- Python实现SYSLOG服务器与MYSQL数据库写入源码,pythonsysl
- python方法判断IP的合法性源码分享,python合法性,用pyt
- Python代码音乐mp3下载器源码分享,python下载器,用pytho
- python方法实现磁盘剩余空间查询操作,python磁盘,想知道
- python转换工具之汉语拼音与国际音标的实现,python转换
- Python方法完成自动合并文本文件操作,python文本文件
- Python math方法算24点代码详解,pythonmath,计算24点的方法
- 如何让Cookie.py模块用冒号做key,cookie.py冒号,python的Co
- Python制作 百度贴吧小爬虫.V0.1,python.v0.1,Python制作 百度
- 常见又实用的Python字符串操作小技巧,python字符串小技
评论关闭