<legend id='iFQ6G'><style id='iFQ6G'><dir id='iFQ6G'><q id='iFQ6G'></q></dir></style></legend>

    1. <tfoot id='iFQ6G'></tfoot>
          <bdo id='iFQ6G'></bdo><ul id='iFQ6G'></ul>

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

        <i id='iFQ6G'><tr id='iFQ6G'><dt id='iFQ6G'><q id='iFQ6G'><span id='iFQ6G'><b id='iFQ6G'><form id='iFQ6G'><ins id='iFQ6G'></ins><ul id='iFQ6G'></ul><sub id='iFQ6G'></sub></form><legend id='iFQ6G'></legend><bdo id='iFQ6G'><pre id='iFQ6G'><center id='iFQ6G'></center></pre></bdo></b><th id='iFQ6G'></th></span></q></dt></tr></i><div id='iFQ6G'><tfoot id='iFQ6G'></tfoot><dl id='iFQ6G'><fieldset id='iFQ6G'></fieldset></dl></div>
      1. 如何通过反向对元组进行排序,但打破非反向关系?(Python)

        How can I sort tuples by reverse, yet breaking ties non-reverse? (Python)(如何通过反向对元组进行排序,但打破非反向关系?(Python))
      2. <tfoot id='Xltiv'></tfoot>

          <tbody id='Xltiv'></tbody>

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

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

                1. 本文介绍了如何通过反向对元组进行排序,但打破非反向关系?(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有一个元组列表:

                  If I have a list of tuples:

                  results = [('10', 'Mary'), ('9', 'John'), ('10', 'George'), ('9', 'Frank'), ('9', 'Adam')]
                  

                  我如何对列表进行排序,就像您在记分牌中看到的那样 - 以便它将分数从大到小排序,但按名称的字母顺序打破平局?

                  How can I sort the list as you might see in a scoreboard - such that it will sort the score from biggest to smallest, but break ties alphabetically by name?

                  所以排序后,列表应该是这样的:

                  So after the sort, the list should look like:

                  results = [('10', 'George'), ('10', 'Mary'), ('9', 'Adam'), ('9', 'Frank'), ('9', 'John')]
                  

                  目前我所能做的就是 results.sort(reverse=True),但也可以按字母顺序颠倒关系...

                  At the moment all I can do is results.sort(reverse=True), but breaks ties reverse alphabetically too...

                  任何帮助将不胜感激.谢谢!

                  Any help would be much appreciated. Thanks!

                  推荐答案

                  实现你想要的最简单的方法是利用python sort 是稳定的这一事实.这允许先按字母顺序排序,然后按分数排序:

                  The simplest way to achieve what you want is to use the fact that python sort is stable. This allows to first sort alphabetically and then by score:

                  In [11]: results = [(10, 'Mary'), (9, 'John'), (10, 'George'), (9, 'Frank'), (9, 'Adam')]
                  
                  In [12]: results.sort(key=lambda x: x[1])
                  
                  In [13]: results.sort(key=lambda x: x[0], reverse=True)
                  
                  In [14]: results
                  Out[14]: [(10, 'George'), (10, 'Mary'), (9, 'Adam'), (9, 'Frank'), (9, 'John')]
                  

                  第一个排序按字母升序排列.第二种排序按分数排序,降序排列,保持分数相等的元素的相对顺序.

                  The first sort sorts alphabetically, in ascending order. The second sort sorts by score, in descending order, maintaining the relative order of elements with equal score.

                  您可以这样做来进行更复杂的排序.请记住,您必须首先按 secondary 键排序,然后再按第一个键.(如果你有三个键,首先按第三个,然后按第二个,最后按主键).

                  You can do this to do even more complex sorts. Just remember that you must first sort by the secondary key, and then by the first key. (If you have three keys, first sort by the third, then by the second, and lastly by the main key).

                  如果您不想调用 sort 两次,则必须编写更复杂的 key 函数.比如:

                  If you don't want to call sort twice you'll have to write a more complex key function. Something like:

                  In [50]: def key(elem):
                      ...:     return elem[0], [-ord(c) for c in elem[1]]
                  
                  In [51]: sorted(results, key=key, reverse=True)
                  Out[51]: [(10, 'George'), (10, 'Mary'), (9, 'Adam'), (9, 'Frank'), (9, 'John')]
                  

                  特别是,每次您有按字典顺序排序的内容(例如字符串、元组、列表等)时,您都可以通过将符号更改为所有元素来反转顺序.

                  In particular, every time you have something sorted in lexicographic order(such as strings, tuples, lists etc.), you can invert the order by changing the sign to all the elements.

                  这篇关于如何通过反向对元组进行排序,但打破非反向关系?(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  • <i id='fjzg1'><tr id='fjzg1'><dt id='fjzg1'><q id='fjzg1'><span id='fjzg1'><b id='fjzg1'><form id='fjzg1'><ins id='fjzg1'></ins><ul id='fjzg1'></ul><sub id='fjzg1'></sub></form><legend id='fjzg1'></legend><bdo id='fjzg1'><pre id='fjzg1'><center id='fjzg1'></center></pre></bdo></b><th id='fjzg1'></th></span></q></dt></tr></i><div id='fjzg1'><tfoot id='fjzg1'></tfoot><dl id='fjzg1'><fieldset id='fjzg1'></fieldset></dl></div>
                    <legend id='fjzg1'><style id='fjzg1'><dir id='fjzg1'><q id='fjzg1'></q></dir></style></legend>

                        <tbody id='fjzg1'></tbody>
                      <tfoot id='fjzg1'></tfoot>

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

                          • <bdo id='fjzg1'></bdo><ul id='fjzg1'></ul>