• <i id='x9aWY'><tr id='x9aWY'><dt id='x9aWY'><q id='x9aWY'><span id='x9aWY'><b id='x9aWY'><form id='x9aWY'><ins id='x9aWY'></ins><ul id='x9aWY'></ul><sub id='x9aWY'></sub></form><legend id='x9aWY'></legend><bdo id='x9aWY'><pre id='x9aWY'><center id='x9aWY'></center></pre></bdo></b><th id='x9aWY'></th></span></q></dt></tr></i><div id='x9aWY'><tfoot id='x9aWY'></tfoot><dl id='x9aWY'><fieldset id='x9aWY'></fieldset></dl></div>

    <small id='x9aWY'></small><noframes id='x9aWY'>

      <bdo id='x9aWY'></bdo><ul id='x9aWY'></ul>

    1. <tfoot id='x9aWY'></tfoot>
      <legend id='x9aWY'><style id='x9aWY'><dir id='x9aWY'><q id='x9aWY'></q></dir></style></legend>
      1. 如何按多个键对对象进行排序?

        How to sort objects by multiple keys?(如何按多个键对对象进行排序?)
          <tbody id='oUQaS'></tbody>

            <i id='oUQaS'><tr id='oUQaS'><dt id='oUQaS'><q id='oUQaS'><span id='oUQaS'><b id='oUQaS'><form id='oUQaS'><ins id='oUQaS'></ins><ul id='oUQaS'></ul><sub id='oUQaS'></sub></form><legend id='oUQaS'></legend><bdo id='oUQaS'><pre id='oUQaS'><center id='oUQaS'></center></pre></bdo></b><th id='oUQaS'></th></span></q></dt></tr></i><div id='oUQaS'><tfoot id='oUQaS'></tfoot><dl id='oUQaS'><fieldset id='oUQaS'></fieldset></dl></div>

          • <legend id='oUQaS'><style id='oUQaS'><dir id='oUQaS'><q id='oUQaS'></q></dir></style></legend>
            1. <small id='oUQaS'></small><noframes id='oUQaS'>

                  <bdo id='oUQaS'></bdo><ul id='oUQaS'></ul>
                  <tfoot id='oUQaS'></tfoot>
                • 本文介绍了如何按多个键对对象进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  或者,实际上,我如何按多个键对词典列表进行排序?

                  我有一个词典列表:

                  b = [{u'TOT_PTS_Misc': u'Utley, Alex', u'Total_Points': 96.0},
                   {u'TOT_PTS_Misc': u'Russo, Brandon', u'Total_Points': 96.0},
                   {u'TOT_PTS_Misc': u'Chappell, Justin', u'Total_Points': 96.0},
                   {u'TOT_PTS_Misc': u'Foster, Toney', u'Total_Points': 80.0},
                   {u'TOT_PTS_Misc': u'Lawson, Roman', u'Total_Points': 80.0},
                   {u'TOT_PTS_Misc': u'Lempke, Sam', u'Total_Points': 80.0},
                   {u'TOT_PTS_Misc': u'Gnezda, Alex', u'Total_Points': 78.0},
                   {u'TOT_PTS_Misc': u'Kirks, Damien', u'Total_Points': 78.0},
                   {u'TOT_PTS_Misc': u'Worden, Tom', u'Total_Points': 78.0},
                   {u'TOT_PTS_Misc': u'Korecz, Mike', u'Total_Points': 78.0},
                   {u'TOT_PTS_Misc': u'Swartz, Brian', u'Total_Points': 66.0},
                   {u'TOT_PTS_Misc': u'Burgess, Randy', u'Total_Points': 66.0},
                   {u'TOT_PTS_Misc': u'Smugala, Ryan', u'Total_Points': 66.0},
                   {u'TOT_PTS_Misc': u'Harmon, Gary', u'Total_Points': 66.0},
                   {u'TOT_PTS_Misc': u'Blasinsky, Scott', u'Total_Points': 60.0},
                   {u'TOT_PTS_Misc': u'Carter III, Laymon', u'Total_Points': 60.0},
                   {u'TOT_PTS_Misc': u'Coleman, Johnathan', u'Total_Points': 60.0},
                   {u'TOT_PTS_Misc': u'Venditti, Nick', u'Total_Points': 60.0},
                   {u'TOT_PTS_Misc': u'Blackwell, Devon', u'Total_Points': 60.0},
                   {u'TOT_PTS_Misc': u'Kovach, Alex', u'Total_Points': 60.0},
                   {u'TOT_PTS_Misc': u'Bolden, Antonio', u'Total_Points': 60.0},
                   {u'TOT_PTS_Misc': u'Smith, Ryan', u'Total_Points': 60.0}]
                  

                  并且我需要使用由Total_Points颠倒的多键排序,然后由TOT_PTS_Misc不颠倒的多键排序。

                  可以在命令提示符下执行此操作,如下所示:

                  a = sorted(b, key=lambda d: (-d['Total_Points'], d['TOT_PTS_Misc']))
                  

                  但我必须通过一个函数运行此操作,在该函数中我传入列表和排序关键字。例如,def multikeysort(dict_list, sortkeys):

                  对于传入到multikeysorte函数的任意数量的键,如何使用lambda行对列表进行排序,并考虑到sortkey可能具有任意数量的键,并且需要反向排序的键将在其前面用‘-’标识?

                  推荐答案

                  此答案适用于字典中的任何类型的列--否定的列不必是数字。

                  def multikeysort(items, columns):
                      from operator import itemgetter
                      comparers = [((itemgetter(col[1:].strip()), -1) if col.startswith('-') else
                                    (itemgetter(col.strip()), 1)) for col in columns]
                      def comparer(left, right):
                          for fn, mult in comparers:
                              result = cmp(fn(left), fn(right))
                              if result:
                                  return mult * result
                          else:
                              return 0
                      return sorted(items, cmp=comparer)
                  

                  您可以这样称呼它:

                  b = [{u'TOT_PTS_Misc': u'Utley, Alex', u'Total_Points': 96.0},
                       {u'TOT_PTS_Misc': u'Russo, Brandon', u'Total_Points': 96.0},
                       {u'TOT_PTS_Misc': u'Chappell, Justin', u'Total_Points': 96.0},
                       {u'TOT_PTS_Misc': u'Foster, Toney', u'Total_Points': 80.0},
                       {u'TOT_PTS_Misc': u'Lawson, Roman', u'Total_Points': 80.0},
                       {u'TOT_PTS_Misc': u'Lempke, Sam', u'Total_Points': 80.0},
                       {u'TOT_PTS_Misc': u'Gnezda, Alex', u'Total_Points': 78.0},
                       {u'TOT_PTS_Misc': u'Kirks, Damien', u'Total_Points': 78.0},
                       {u'TOT_PTS_Misc': u'Worden, Tom', u'Total_Points': 78.0},
                       {u'TOT_PTS_Misc': u'Korecz, Mike', u'Total_Points': 78.0},
                       {u'TOT_PTS_Misc': u'Swartz, Brian', u'Total_Points': 66.0},
                       {u'TOT_PTS_Misc': u'Burgess, Randy', u'Total_Points': 66.0},
                       {u'TOT_PTS_Misc': u'Smugala, Ryan', u'Total_Points': 66.0},
                       {u'TOT_PTS_Misc': u'Harmon, Gary', u'Total_Points': 66.0},
                       {u'TOT_PTS_Misc': u'Blasinsky, Scott', u'Total_Points': 60.0},
                       {u'TOT_PTS_Misc': u'Carter III, Laymon', u'Total_Points': 60.0},
                       {u'TOT_PTS_Misc': u'Coleman, Johnathan', u'Total_Points': 60.0},
                       {u'TOT_PTS_Misc': u'Venditti, Nick', u'Total_Points': 60.0},
                       {u'TOT_PTS_Misc': u'Blackwell, Devon', u'Total_Points': 60.0},
                       {u'TOT_PTS_Misc': u'Kovach, Alex', u'Total_Points': 60.0},
                       {u'TOT_PTS_Misc': u'Bolden, Antonio', u'Total_Points': 60.0},
                       {u'TOT_PTS_Misc': u'Smith, Ryan', u'Total_Points': 60.0}]
                  
                  a = multikeysort(b, ['-Total_Points', 'TOT_PTS_Misc'])
                  for item in a:
                      print item
                  

                  尝试其中任何一列都被否定。您将看到排序顺序颠倒。

                  下一步:更改它,使其不使用额外的类.


                  2016-01-17

                  我从这个答案中得到灵感What is the best way to get the first item from an iterable matching a condition?,我缩短了代码:

                  from operator import itemgetter as i
                  
                  def multikeysort(items, columns):
                      comparers = [
                          ((i(col[1:].strip()), -1) if col.startswith('-') else (i(col.strip()), 1))
                          for col in columns
                      ]
                      def comparer(left, right):
                          comparer_iter = (
                              cmp(fn(left), fn(right)) * mult
                              for fn, mult in comparers
                          )
                          return next((result for result in comparer_iter if result), 0)
                      return sorted(items, cmp=comparer)
                  

                  以防您喜欢代码简洁。


                  之后2016-01-17

                  这适用于python3(它消除了sortcmp参数):

                  from operator import itemgetter as i
                  from functools import cmp_to_key
                  
                  def cmp(x, y):
                      """
                      Replacement for built-in function cmp that was removed in Python 3
                  
                      Compare the two objects x and y and return an integer according to
                      the outcome. The return value is negative if x < y, zero if x == y
                      and strictly positive if x > y.
                  
                      https://portingguide.readthedocs.io/en/latest/comparisons.html#the-cmp-function
                      """
                  
                      return (x > y) - (x < y)
                  
                  def multikeysort(items, columns):
                      comparers = [
                          ((i(col[1:].strip()), -1) if col.startswith('-') else (i(col.strip()), 1))
                          for col in columns
                      ]
                      def comparer(left, right):
                          comparer_iter = (
                              cmp(fn(left), fn(right)) * mult
                              for fn, mult in comparers
                          )
                          return next((result for result in comparer_iter if result), 0)
                      return sorted(items, key=cmp_to_key(comparer))
                  

                  受此答案启发How should I do custom sort in Python 3?

                  这篇关于如何按多个键对对象进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                      <small id='qM72p'></small><noframes id='qM72p'>

                      <tfoot id='qM72p'></tfoot>

                          <bdo id='qM72p'></bdo><ul id='qM72p'></ul>
                          <i id='qM72p'><tr id='qM72p'><dt id='qM72p'><q id='qM72p'><span id='qM72p'><b id='qM72p'><form id='qM72p'><ins id='qM72p'></ins><ul id='qM72p'></ul><sub id='qM72p'></sub></form><legend id='qM72p'></legend><bdo id='qM72p'><pre id='qM72p'><center id='qM72p'></center></pre></bdo></b><th id='qM72p'></th></span></q></dt></tr></i><div id='qM72p'><tfoot id='qM72p'></tfoot><dl id='qM72p'><fieldset id='qM72p'></fieldset></dl></div>
                              <tbody id='qM72p'></tbody>
                            <legend id='qM72p'><style id='qM72p'><dir id='qM72p'><q id='qM72p'></q></dir></style></legend>