`
yugouai
  • 浏览: 492048 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Python map排序

阅读更多

Abstract

    This PEP suggests a "sort by value" operation for dictionaries.
    The primary benefit would be in terms of "batteries included"
    support for a common Python idiom which, in its current form, is
    both difficult for beginners to understand and cumbersome for all
    to implement.

BDFL Pronouncement

    This PEP is rejected because the need for it has been largely
    fulfilled by Py2.4's sorted() builtin function:

        >>> sorted(d.iteritems(), key=itemgetter(1), reverse=True)
        [('b', 23), ('d', 17), ('c', 5), ('a', 2), ('e', 1)]

    or for just the keys:

        sorted(d, key=d.__getitem__, reverse=True)
        ['b', 'd', 'c', 'a', 'e']

    Also, Python 2.5's heapq.nlargest() function addresses the common use
    case of finding only a few of the highest valued items:

        >>> nlargest(2, d.iteritems(), itemgetter(1))
        [('b', 23), ('d', 17)]


Motivation

    A common use of dictionaries is to count occurrences by setting
    the value of d[key] to 1 on its first occurrence, then increment
    the value on each subsequent occurrence.  This can be done several
    different ways, but the get() method is the most succinct:

            d[key] = d.get(key, 0) + 1

    Once all occurrences have been counted, a common use of the
    resulting dictionary is to print the occurrences in
    occurrence-sorted order, often with the largest value first.

    This leads to a need to sort a dictionary's items by value.  The
    canonical method of doing so in Python is to first use d.items()
    to get a list of the dictionary's items, then invert the ordering
    of each item's tuple from (key, value) into (value, key), then
    sort the list; since Python sorts the list based on the first item
    of the tuple, the list of (inverted) items is therefore sorted by
    value.  If desired, the list can then be reversed, and the tuples
    can be re-inverted back to (key, value).  (However, in my
    experience, the inverted tuple ordering is fine for most purposes,
    e.g. printing out the list.)

    For example, given an occurrence count of:

        >>> d = {'a':2, 'b':23, 'c':5, 'd':17, 'e':1}

    we might do:

        >>> items = [(v, k) for k, v in d.items()]
        >>> items.sort()
        >>> items.reverse()             # so largest is first
        >>> items = [(k, v) for v, k in items]

    resulting in:

        >>> items
        [('b', 23), ('d', 17), ('c', 5), ('a', 2), ('e', 1)]

    which shows the list in by-value order, largest first.  (In this
    case, 'b' was found to have the most occurrences.)

    This works fine, but is "hard to use" in two aspects.  First,
    although this idiom is known to veteran Pythoneers, it is not at
    all obvious to newbies -- either in terms of its algorithm
    (inverting the ordering of item tuples) or its implementation
    (using list comprehensions -- which are an advanced Python
    feature.)  Second, it requires having to repeatedly type a lot of
    "grunge", resulting in both tedium and mistakes.

    We therefore would rather Python provide a method of sorting
    dictionaries by value which would be both easy for newbies to
    understand (or, better yet, not to _have to_ understand) and
    easier for all to use.


Rationale

    As Tim Peters has pointed out, this sort of thing brings on the
    problem of trying to be all things to all people.  Therefore, we
    will limit its scope to try to hit "the sweet spot".  Unusual
    cases (e.g. sorting via a custom comparison function) can, of
    course, be handled "manually" using present methods.

    Here are some simple possibilities:

    The items() method of dictionaries can be augmented with new
    parameters having default values that provide for full
    backwards-compatibility:

        (1) items(sort_by_values=0, reversed=0)

    or maybe just:

        (2) items(sort_by_values=0)

    since reversing a list is easy enough.

    Alternatively, items() could simply let us control the (key, value) 
    order:

        (3) items(values_first=0)

    Again, this is fully backwards-compatible.  It does less work than
    the others, but it at least eases the most complicated/tricky part
    of the sort-by-value problem: inverting the order of item tuples.
    Using this is very simple:

        items = d.items(1)
        items.sort()
        items.reverse()         # (if desired)

    The primary drawback of the preceding three approaches is the
    additional overhead for the parameter-less "items()" case, due to
    having to process default parameters.  (However, if one assumes
    that items() gets used primarily for creating sort-by-value lists,
    this is not really a drawback in practice.)

    Alternatively, we might add a new dictionary method which somehow
    embodies "sorting".  This approach offers two advantages.  First,
    it avoids adding overhead to the items() method.  Second, it is
    perhaps more accessible to newbies: when they go looking for a
    method for sorting dictionaries, they hopefully run into this one,
    and they will not have to understand the finer points of tuple
    inversion and list sorting to achieve sort-by-value.

    To allow the four basic possibilities of sorting by key/value and in 
    forward/reverse order, we could add this method:

        (4) sorted_items(by_value=0, reversed=0)

    I believe the most common case would actually be "by_value=1,
    reversed=1", but the defaults values given here might lead to
    fewer surprises by users: sorted_items() would be the same as
    items() followed by sort().

    Finally (as a last resort), we could use:

        (5) items_sorted_by_value(reversed=0)


Implementation

    The proposed dictionary methods would necessarily be implemented
    in C.  Presumably, the implementation would be fairly simple since
    it involves just adding a few calls to Python's existing
    machinery.


Concerns

    Aside from the run-time overhead already addressed in
    possibilities 1 through 3, concerns with this proposal probably
    will fall into the categories of "feature bloat" and/or "code
    bloat".  However, I believe that several of the suggestions made
    here will result in quite minimal bloat, resulting in a good
    tradeoff between bloat and "value added".

    Tim Peters has noted that implementing this in C might not be
    significantly faster than implementing it in Python today.
    However, the major benefits intended here are "accessibility" and
    "ease of use", not "speed".  Therefore, as long as it is not
    noticeably slower (in the case of plain items(), speed need not be
    a consideration.


References

    A related thread called "counting occurrences" appeared on
    comp.lang.python in August, 2001.  This included examples of
    approaches to systematizing the sort-by-value problem by
    implementing it as reusable Python functions and classes.


Copyright

    This document has been placed in the public domain.

linked:https://www.python.org/dev/peps/pep-0265/
分享到:
评论

相关推荐

    python ip地址排序算法2.0

    重写了原来的ip地址排序算法,可扩充到任意分组排序,同时组间排序。采用了map,lambda和递归函数,计算时间可大幅提交,逻辑比较清晰,如果还可以简化,请大神指导,谢谢

    python对字典进行排序实例

    本文实例讲述了python对字典进行排序的方法,是非常实用的技巧。分享给大家供大家参考。 具体实现方法如下: import itertools thekeys = ['b','a','c'] thevalues = ['bbb','aaa','cccc'] d = dict(itertools....

    Python将列表中的元素转化为数字并排序的示例

    本文实例讲述了Python中列表元素转为数字的方法。...2. Python3.x,map返回的是map对象,当然也可以转换为List: numbers = list(map(int, numbers)) 排序: 使用sorted函数,从小到大排序: numbers = sorted(number

    Python中sorted函数、filter类、map类、reduce函数

    Python中使用函数作为参数的内置函数和类: 函数名或类名 功能 参数描述 sorted函数 用来将一个无序列表(元组)进行排序 函数参数的返回值规定按照元素的哪个属性进行排序 filter类 用来过滤一个列表里符合...

    蓝桥杯之数列排序问题python实现

    蓝桥杯之数列排序问题python实现 题目 问题描述 给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200 输入格式 第一行为一个整数n。  第二行包含n个整数,为待排序的数,每个整数的绝对值小于...

    基于Python实现的数据结构与算法完整源代码+超详细注释(包含46个作业项目).zip

    26_Hash散列&ADT Map 27_树的嵌套列表实现 28_树结构的节点链接法实现 29_表达式解析树 30_树的遍历 31_python实现ADT BinaryHeap 32_二叉查找树 33_AVL树的python实现 34_python实现ADT Graph 35_词梯WordLadder...

    Python 对输入的数字进行排序的方法

    要求,输入一串数字,并以列表的形式打印出来。 number = input('请输入一串数字:') print(number) print(type(number)) 假设输入12345,得到结果如下: 请输入一串数字:12345 ...print(list(map(int,list

    python入门到高级全栈工程师培训 第3期 附课件代码

    python入门到高级全栈工程师培训视频学习资料;本资料仅用于学习,请查看后24小时之内删除。 【课程内容】 第1章 01 计算机发展史 02 计算机系统 03 小结 04 数据的概念 05 进制转换 06 原码补码反码 07 物理层和...

    Python入门知识经典总结.docx

    使用heapq模块进行高效的最大或最小堆排序,如heapq.nlargest(n, iterable)或heapq.nsmallest(n, iterable)来获取列表中的前n个最大或最小元素。 函数与闭包: 创建闭包以保存外部函数的状态,确保即使外部函数执行...

    python实现bitmap数据结构详解

    用于无重复整数的排序等等。bitmap通常基于数组来实现,数组中每个元素可以看成是一系列二进制数,所有元素组成更大的二进制集合。对于Python来说,整数类型默认是有符号类型,所以一个整数的可用位数为31位。bitmap...

    Python的高阶函数用法实例分析

    本文实例讲述了Python的高阶函数用法。分享给大家供大家参考,具体如下: 高阶函数 1.MapReduce MapReduce主要应用于分布式中。 大数据实际上是在15年下半年开始火起来的。...#python内置了map()和reduce

    Python学习总结2

    Series/DataFrame/读取与导出/访问与筛选/轴、合并、连接/排序与匿名函数/分组、聚合、转换/常用字符串方法/绘图/map、apply、applymap

    python基础3day01.txt

    # 按字符顺序排序,不区分大小写 def f(ch): code = ord(ch) # 得到编码 if (97+26) >code >= 97: code -= 32 return code sorted('ACDacbdE', key=f) # ['A', 'a', 'b', 'C', 'c', 'D', '

    基于Java和Python的爬虫项目实战源码.zip

    使用非极大值抑制法确定镜头边界系数极大值并排序,以实现基于镜头边界系数的关键帧提取 JMF(Java视频处理): 功能 a)在Java Applet和应用程序中播放贵重物品媒体文件,如AVI、MPEG、WAV等; b)可以播放从互联网...

    leetcode中325题python-leetcode:leetcode刷题

    leetcode中325题python leetcode刷题 6月13日 1021, 921 6月17日 刷题日,刷15题 98, 236, 235, 15, 703 二叉树遍历: pre_order, in_order, post_order 广度优先遍历:队列实现,先进先出,还可以有个visited的集合...

    Python中的匿名函数和函数式编程

    Python中的匿名函数和函数式编程 文章目录Python中的匿名函数和函数式编程一、匿名函数匿名函数的格式:二、函数式编程map()filter()reduce()区别三、‘三目...# 对字典中的key/value,根据value进行从大到小排序: di

    algorithm_record_byPython:python刷题记录

    algorithm_record_byPython ...n≤100000n≤100000 => O(nlogn)O(nlogn) => 各种sort,线段树、树状数组、set/map、heap、拓扑排序、dijkstra+heap、prim+heap、spfa、求凸包、求半平面交、二分 n≤10

    leetcode解码方法Python-LeetCode:跟踪LeetCode进度

    :world_map: 数据结构/方法总结 广度优先搜索 (BFS) / 深度优先搜索 (DFS) Python:不能使用[[0]*n]*m创建列表列表!!! 这些列表将引用相同的 ID! 哈希表 Python:在 Python 2 中,dictionary.keys() 返回一个...

    Python代码实现删除一个list里面重复元素的方法

    方法一:是利用map的fromkeys来自动过滤重复值,map是基于hash的,大数组的时候应该会比排序快点吧 方法二:是用set(),set是定义集合的,无序,非重复 方法三:是排序后,倒着扫描,遇到已有的元素删之 #!/usr/bin/...

    Python中sort和sorted函数代码解析

    本文研究的主要是Python中sort和sorted函数的相关内容,具体如下。 一、sort函数 sort函数是序列的内部函数 函数原型: L.sort(cmp=None, key=None, reverse=False) 函数作用: 它是把L原地排序,也就是使用后并不是...

Global site tag (gtag.js) - Google Analytics