`
yugouai
  • 浏览: 498569 次
  • 性别: 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

    在这个"python ip地址排序算法2.0"中,开发者显然已经对原始的排序算法进行了优化,旨在提高效率并支持更复杂的排序需求,比如按IP地址的分组进行排序。这个更新的算法利用了Python中的map函数、lambda表达式以及...

    Python-sorta帮助你对文件进行排序

    本文将深入探讨Python中的文件操作,以及如何利用类似"sorta"的工具来高效地对文件进行排序。 首先,了解Python的基本文件操作是必要的。在Python中,我们可以使用内置的`open()`函数打开文件,然后通过`read()`、`...

    Python面试宝典.pdf

    2. map函数:了解Python中map函数的使用,包括对列表、字典的操作等。 3. filter函数:了解Python中filter函数的使用,包括对列表、字典的操作等。 装饰器 1. 了解Python中装饰器的基本概念,包括装饰器的定义、...

    python对字典进行排序实例

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

    Python基础训练100题(带答案).docx

    ### Python基础训练100题知识点解析 #### 实例001:数字组合 - **题目**:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三...通过这些实例的学习,可以加深对Python语言的理解,并提高解决问题的能力。

    python字典排序实例详解

    本篇文章将详细介绍如何在Python中对字典进行排序,并提供四种不同的实现方法。 首先,我们需要理解Python字典的一些基本特性: 1. 字典的键(keys)是唯一的,且大小写敏感。 2. 字典中不允许有重复的键。 3. ...

    C语言中文网Python教程

    “7.22Python函数式编程(map()、filter()和reduce())详解.html”介绍了Python的函数式编程概念,其中map()函数将一个函数应用于列表的每个元素并返回结果列表,filter()函数根据指定条件过滤列表元素,reduce()...

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

    在Python编程语言中,对输入的数字进行排序是一项常见的任务,尤其在处理用户输入或数据处理时。本文将详细介绍如何使用Python对输入的数字进行排序,并通过实例代码进行解析。 首先,用户通常会通过`input()`函数...

    python学习资料和课件,python教学课件,Python

    3. **列表排序**: 使用sort()函数或内置的sorted()函数对列表进行排序,以及reverse()方法翻转列表。 4. **元组(Tuple)**: 不可变数据结构,常用于创建不可修改的序列,以及在函数中作为返回值。 5. **集合(Set...

    Python开发mapreduce的demo

    2. **Shuffle阶段**:在Map阶段完成后,系统会按照键对中间结果进行排序和分区,准备进入Reduce阶段。 3. **Reduce阶段**:在这个阶段,Reduce函数对每个键的中间结果进行聚合操作。Python中,可以使用`reduce()`...

    Python实现排序的KNN算法.zip

    distance = list(map(lambda x: abs(predictpoint - x), feature)) index = np.argsort(distance) sortedlable = (label[index]) return c.Counter(sortedlable[0:k]).most_common(1)[0][0]

    福建农林大学[python实验]

    python实验题目集锦 这篇文章总结了福建农林大学python实验的四个题目,包括输出m,n...这四个题目涵盖了python基础语法、数据结构、算法和字符串处理等多方面的知识点,旨在考察学生对python语言的理解和应用能力。

    python_NOC复赛模拟考文件.zip

    2. **数据结构**:Python中的列表、元组、集合和字典等,如何创建、访问、修改和遍历这些数据结构,以及它们在解决问题时的应用,比如排序、查找、去重等操作。 3. **算法**:基础算法如排序(冒泡排序、插入排序、...

    hdu杭电所有题目按照ac数量排序,python分析

    综上所述,通过对HDOJ平台上题目按AC数量排序的分析,不仅能够了解不同类型的题目特点,还能通过具体的Python示例代码加深对编程技术和解题策略的理解。此外,结合数据可视化和机器学习等进阶技术的应用,可以为平台...

    最新Python学习笔记3

    它是Python中内置的高阶函数之一,除了可以直接对列表进行排序之外,还可以接收一个key函数参数来实现自定义排序。key函数在排序过程中会对每个元素进行处理,然后根据处理后的结果进行排序。例如,可以使用sorted()...

    Python二级考试资料

    在Python二级考试中,考生通常需要展示对Python语言的深入理解,包括但不限于以下几个方面: 1. **基础语法**:这涵盖了变量定义、数据类型(如整型、浮点型、字符串、列表、元组、字典和集合)、流程控制(如条件...

    python-3.7.7-docs-pdf-a4.zip

    在Python中,有多种方式可以对数据进行排序,如内置的`sorted()`函数、列表的`sort()`方法,以及`heapq`模块等。这一部分将详细解释这些方法的工作原理,如何使用它们对不同类型的数据(包括自定义对象)进行排序,...

    Python编程500例快速构建可执行高质量代码.pptx

    3、算法:本书对Python中的算法进行了深入探讨,涵盖了排序算法、搜索算法、图算法等方面的内容。读者可以通过学习这些内容快速掌握Python中的算法。 4、文件处理:本书对Python中的文件处理进行了深入探讨,涵盖了...

    Python 案例.zip

    2. **数据结构与函数**:这里会深入讲解Python中的列表、元组、字典和集合的操作,包括增删改查以及排序等方法。同时,函数的定义、调用、参数传递、匿名函数(lambda)以及高阶函数(如map、filter、reduce)也会被...

Global site tag (gtag.js) - Google Analytics