zoukankan      html  css  js  c++  java
  • Python: 字典列表: itemgetter 函数: 根据某个或某几个字典字段来排序列表

    问题:根据某个或某几个字典字段来排序Python列表

    answer: 通过使用operator 模块的itemgetter 函数,可以非常容易的排序这样的数据结构

    eg:

      rows = [
      {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
      {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
      {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
      {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
      ]

      1.itemgetter()

      from operator import itemgetter

      1).print(rows_by_fname)

      >>>rows_by_fname = sorted(rows, key=itemgetter('fname'))

      >>>print(rows_by_fname)

      [{'fname': 'Big', 'uid': 1004, 'lname': 'Jones'},
      {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'},
      {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'},
      {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'}]

      2).print(rows_by_uid) 

      >>>rows_by_uid = sorted(rows, key=itemgetter('uid'))

      >>>print(rows_by_uid)
      [{'fname': 'John', 'uid': 1001, 'lname': 'Cleese'},
      {'fname': 'David', 'uid': 1002, 'lname': 'Beazley'},
      {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'},
      {'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}]

      3).itemgetter() 函数也支持多个keys

      itemgetter() 函数也支持多个keys,比如下面的代码
      >>>rows_by_lfname = sorted(rows, key=itemgetter('lname','fname'))
      >>>print(rows_by_lfname)

      [{'fname': 'David', 'uid': 1002, 'lname': 'Beazley'},
      {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'},
      {'fname': 'Big', 'uid': 1004, 'lname': 'Jones'},
      {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}]

      在上面例子中,rows被传递给接受一个关键字参数的sorted()内置函数。

      这个参数是callable类型,并且从rows中接受一个单一元素,然后返回被用来排序的值。

      itemgetter()就是负责创建这个callable对象的。

      如果传多个参数给itemgetter(),它生成的callabe对象会返回一个包含所有值的元组,sort()函数会根据元组中元素顺序去排序。

      2.lambda表达式

        itemgetter()有时候可以用lambda表达式代替

        rows_by_fname = sorted(rows, key=lambda r: r['fname'])
        rows_by_lfname = sorted(rows, key=lambda r: (r['lname'],r['fname']))

        这种方案也不错。但是,使用itemgetter() 方式会运行的稍微快点。因此,如果
        你对性能要求比较高的话就使用itemgetter() 方式。

      3.min(),max()

        最后,不要忘了这节中展示的技术也同样适用于min() 和max() 等函数。比如:
        >>> min(rows, key=itemgetter('uid'))
        {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}
        >>> max(rows, key=itemgetter('uid'))
        {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}

  • 相关阅读:
    matplotlib油漆基础
    使用ant编译项目技能
    [Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose
    [Recompose] Make Reusable React Props Streams with Lenses
    [Recompose] Compose Streams of React Props with Recompose’s compose and RxJS
    [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream
    [Recompose] Stream Props to React Children with RxJS
    [Recompose] Merge RxJS Button Event Streams to Build a React Counter Component
    [Recompose] Handle React Events as Streams with RxJS and Recompose
    [Recompose] Stream a React Component from an Ajax Request with RxJS
  • 原文地址:https://www.cnblogs.com/baili-luoyun/p/11217117.html
Copyright © 2011-2022 走看看