需求

K长的序列,求TopN

K长的序列,求BtmN

排序问题

解决

  • heap.nlargest()、heap.nsmallest( )
  • sorted( )+切片
  • max( )、min( )

总结和比较

1)在Top N问题中,如果 N=1,则直接用max(iterable)/min(iterable) 即可(效率最高)。

2)如果N很大,接近集合元素,则为了提高效率,采用 sort+切片 的效率会更高,如:

 求最大的N个元素:sorted(iterable, key=key, reverse=True)[:N]

 求最小的N个元素:sorted(iterable, key=key)[:N]

3)当要查找的元素个数相对比较小的时候,使用 nlargest() 和 nsmallest() 是很合适的

详解max( )/min( )函数用法

  • 求简单的序列TopN/BtmN(N=1)问题
lst=[1,2,3,4,5]
print(max(lst))
5
  • 通过key属性的使用,设置函数条件为判断的标准
a=[-9,-8,1,3,-4,6]
print(max(a,key=lambda x:abs(x)))
-9
  • 找出字典中值最大的那组数据
prices = {
'A':123,
'B':450.1,
'C':12,
'E':444,
}
//在对字典进行数据操作的时候,默认只会处理key,而不是value
//先使用zip把字典的keys和values翻转过来,再用max取出值最大的那组数据
max_prices=max(zip(prices.values(),prices.keys()))
print(max_prices)
(450.1, 'B')

nlargest( )/nsmallest( )详解

  • nlargest(n,iterable) 求序列iterable中的TopN | nsmallest(n,iterable) 求序列iterable中的BtmN
import heapq
nums=[16,7,3,20,17,8,-1]
print(heapq.nlargest(3,nums))
print(heapq.nsmallest(3,nums))
[20, 17, 16]
[-1, 3, 7]
  • nlargest(n, iterable, key=lambda) | nsmallest(n, iterable, key=lambda) key接受关键字参数,用于更复杂的数据结构中
def print_price(dirt):
for i in dirt:
for x,y in i.items():
if x=='price':
print(x,y)
portfolio = [
{'name': 'IBM', 'shares': 100, 'price': 91.1},
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
{'name': 'FB', 'shares': 200, 'price': 21.09},
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
{'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap=heapq.nsmallest(3,portfolio,key=lambda x:x['price'])
expensive=heapq.nlargest(3,portfolio,key=lambda y:y['price'])
print_price(cheap)
print_price(expensive)
price 16.35
price 21.09
price 31.75
price 543.22
price 115.65
price 91.1

sorted( )详解

  • sorted(iterable, key=None, reverse=False)
  • reverse=True 逆序
nums=[16,7,3,20,17,8,-1]
print(sorted(nums))
print(sorted(nums,reverse=True))
[-1, 3, 7, 8, 16, 17, 20]
[20, 17, 16, 8, 7, 3, -1]
str=['b','a','A','s']
print(sorted(str))
print(sorted(str,reverse=True))
['A', 'a', 'b', 's']
['s', 'b', 'a', 'A']
  • key接受一个函数,且是个只接受一个元素的函数
  • 多条件的key应该怎么写?
//按长度排序
L = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]
print(sorted(L,key=lambda x: len(x)))
[{1: 9}, {1: 5, 3: 4}, {1: 3, 6: 3}, {1: 1, 2: 4, 5: 6}]
//根据指定的值来排序(例如字典中的某个key)

L = [

('john', 'A', 15),

('jane', 'B', 10),

('dave', 'B', 12),

]

print(sorted(L,key=lambda x:x[2],reverse=True))

[('john', 'A', 15), ('dave', 'B', 12), ('jane', 'B', 10)]

portfolio = [

{'name': 'IBM', 'shares': 100, 'price': 91.1},

{'name': 'AAPL', 'shares': 50, 'price': 543.22},

{'name': 'FB', 'shares': 200, 'price': 21.09},

{'name': 'HPQ', 'shares': 35, 'price': 31.75},

{'name': 'YHOO', 'shares': 45, 'price': 16.35},

{'name': 'ACME', 'shares': 75, 'price': 115.65}

]

print(sorted(portfolio,key=lambda x:x['price']))

[{'shares': 45, 'name': 'YHOO', 'price': 16.35}, {'shares': 200, 'name': 'FB', 'price': 21.09}, {'shares': 35, 'name': 'HPQ', 'price': 31.75}, {'shares': 100, 'name': 'IBM', 'price': 91.1}, {'shares': 75, 'name': 'ACME', 'price': 115.65}, {'shares': 50, 'name': 'AAPL', 'price': 543.22}]
//不规则字符串,按“小写-大写-奇数-偶数”顺序排序

s = 'asdf234GDSdsf23'

print("".join(sorted(s, key=lambda x: (x.isdigit(),x.isdigit() and int(x) % 2 == 0,x.isupper(),x))))

addffssDGS33224
//一道面试题:要求:正数在前负数在后 2.整数从小到大 3.负数从大到小

list1=[7, -8, 5, 4, 0, -2, -5]

print(sorted(list1,key=lambda x:(x<0,abs(x))))

[0, 4, 5, 7, -2, -5, -8]

  • 用operator中的函数加快速度和进行多级排序
from operator import itemgetter, attrgetter
暂不讨论

比较三种方法的效率

  • 只求TopN=1/BtmN=1时,比较max( )和nlargest( )两种效率
In [8]: nums=random.sample(range(1,10000),999)
In [9]: print(max(nums))
9999
In [10]: %time
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 13.1 µs
In [11]: heapq.nlargest(1,nums)
Out[11]: [9999]
In [12]: %time
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 14.1 µs
  • 当K为10,N为9(即N无限接近K时),比较了sorted( )+切片和nlargest( )两种方法的效率
In [23]: nums=random.sample(range(1,10000),10)
In [24]: sorted(nums,reverse=True)[:9]
Out[24]: [8814, 7551, 7318, 5597, 5257, 4437, 4211, 2776, 2440]
In [25]: %time
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 11.4 µs
In [26]: heapq.nlargest(9,nums)
Out[26]: [8814, 7551, 7318, 5597, 5257, 4437, 4211, 2776, 2440]
In [27]: %time
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 154 µs
  • 当N较小时,比较了nlargest( )和sorted( )+切片两种方法
In [18]: nums=[16,7,3,20,17,8,-1]
In [19]: heapq.nlargest(3,nums)
Out[19]: [20, 17, 16]
In [20]: %time
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 4.05 µs
In [21]: sorted(nums,reverse=True)[:3]
Out[21]: [20, 17, 16]
In [22]: %time
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 5.48 µs

以上代码用到的import和show_tree( )

import math
import io
from io import StringIO
import heapq
import random
import time
from functools import wraps
def show_tree(tree, total_width=36, fill=' '):
output =io.StringIO() #创建stringio对象
last_row = -1
for i, n in enumerate(tree): #
if i:
row = int(math.floor(math.log(i+1, 2)))
else:
row = 0
if row != last_row:
output.write('\n')
columns = 2**row
col_width = int(math.floor((total_width * 1.0) / columns))
output.write(str(n).center(col_width, fill))
last_row = row
print(output.getvalue())
print('-' * total_width)
print(' ')
return

参考资料

python3-cookbook-1.4-查找最大或最小元素

详解Python中heapq模块的用法(这里有实现show_tree的函数代码)

理解堆和堆排序的文章1

理解堆和堆排序的文章2

理解堆和堆排序的文章3

python奇技淫巧——max/min函数的用法

[PY3]——求TopN/BtmN 和 排序问题的解决的更多相关文章

  1. @NamedEntityGraphs --JPA按实体类对象参数中的字段排序问题得解决方法

    JPA按实体类对象参数中的字段排序问题得解决方法@Entity @Table(name="complaints") @NamedEntityGraphs({ @NamedEntit ...

  2. 第2节 网站点击流项目(下):3、流量统计分析,分组求topN

    四. 模块开发----统计分析 select * from ods_weblog_detail limit 2;+--------------------------+---------------- ...

  3. js关于对象键值为数字型时输出的对象自动排序问题的解决方法

    一.对象键值为数字型时输出的对象自动排序问题如: var objs = {    "1603":{id:"1603"},    "1702" ...

  4. Hadoop学习之路(二十)MapReduce求TopN

    前言 在Hadoop中,排序是MapReduce的灵魂,MapTask和ReduceTask均会对数据按Key排序,这个操作是MR框架的默认行为,不管你的业务逻辑上是否需要这一操作. 技术点 MapR ...

  5. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  6. hive求TopN语句

    ROW_NUMBER,RANK(),DENSE_RANK() 先了解这三个之间的区别: Rank():1,2,2,4,5(一般用这个较多,不会影响总排名) Dense_rank():1,2,2,3,4 ...

  7. sicily 1046. Plane Spotting(排序求topN)

    DescriptionCraig is fond of planes. Making photographs of planes forms a major part of his daily lif ...

  8. SQL分组求每组最大值问题的解决方法收集 (转载)

    例如有一个表student,其结构如下: id      name     sort      score 1        张三      语文      82 2        李四      数 ...

  9. 生成ansible-playbook的yaml文件的代码(字典排序问题无法解决)

    import yaml import collections def add_task(): return None def add_vars(): return None def add_handl ...

随机推荐

  1. First App on Phonegap | Cordova

    Phonegap简介 PhoneGap是一能够让你用普通的web技术编写出能够轻松调用api接口和进入应用商店的 html5应用开发平台,是唯一支持7个平台的开源移动框架. 优势: 1.兼容性:多平台 ...

  2. vue的props 属性类似于bug的东西

    /* * @Author: shs * @Date: 2019-04-19 17:48:39 * @Last Modified by: shs * @Last Modified time: 2019- ...

  3. Excel转Json工具

    应用程序在本地的数据配置的格式一般有JSON.XML.YAML.INI等格式,但是如果直接编写JSON数据往往不是特别方便, 今天给大家分享的是如何在EXCEL配置好数据,然后一键转换成JSON和C# ...

  4. c# 类的反射实例 (GetType().Invoke().GetMethod().CreateInstance())

    原文:http://www.cnblogs.com/chenwei19/archive/2009/02/04/1384034.html Class1和Form 窗体在同一个命名空间 using Sys ...

  5. WebService 常用的设置

    1.修改WebService接收长度 <binding name="IAuthServiceSoap11Binding" maxBufferSize="214748 ...

  6. 使用git命令提示“不是内部或外部命令

    问题描述: 打开windows的cmd,在里面打git命令会提示“不是内部或外部命令,也不是可运行的程序” 解决办法: 找到git安装路径中bin的位置,如:D:\Program Files\Git\ ...

  7. 279. 完全平方数 leetcode JAVA

    题目: 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释: ...

  8. Windows下Oracle的下载与安装及配置

    一.Oracle下载 官网地址:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html 百 ...

  9. 修复已损坏的交换机IMG

    1.找出与当前交换机匹配的升级镜像以及合适的BOOT启动程序(这要通过渠道获取) 2.搭建 TFTP 服务器. 3.启动交换机,链接console线缆 4.在 BOOT 模式,完成一下操作 1.配置I ...

  10. Ionic无法通过npm安装解决方案

    http://www.jianshu.com/p/5a99334eb62d 一般从 node.js官网下载安装完之后,npm也会同时安装完. 如果通过 $ npm install -g cordova ...