<bdo id='OJONu'></bdo><ul id='OJONu'></ul>
<legend id='OJONu'><style id='OJONu'><dir id='OJONu'><q id='OJONu'></q></dir></style></legend>

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

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

      使用两个键对 Python 列表进行排序,但只有一个键以相反的顺序排列

      时间:2023-09-01
        • <legend id='U10tH'><style id='U10tH'><dir id='U10tH'><q id='U10tH'></q></dir></style></legend>

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

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

                <tfoot id='U10tH'></tfoot>
              1. 本文介绍了使用两个键对 Python 列表进行排序,但只有一个键以相反的顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想知道通过两个键对元组列表进行排序的 Pythonic 方法是什么,其中使用一个(并且只有一个)键进行排序将是相反的顺序,而使用另一个键进行排序将不区分大小写.更具体地说,我有一个包含元组的列表,例如:

                I was wondering what would be a Pythonic way of sorting a list of tuples by two keys whereby sorting with one (and only one) key would be in a reverse order and sorting with the the other would be case insensitive. More specifically, I have a list containing tuples like:

                myList = [(ele1A, ele2A),(ele1B, ele2B),(ele1C, ele2C)]
                

                我可以使用下面的代码用两个键对其进行排序:

                I can use the following code to sort it with two keys:

                sortedList = sorted(myList, key = lambda y: (y[0].lower(), y[1]))
                

                我可以使用倒序排序

                sortedList = sorted(myList, key = lambda y: (y[0].lower(), y[1]), reverse = True)
                

                但这将使用两个键以相反的顺序排序.

                but this would sort in a reverse order with two keys.

                推荐答案

                当我们需要对具有两个约束的列表进行排序时,将使用两个键,一个按升序排列,另一个按降序排列在同一个列表中或任何
                在您的示例中
                sortedList = sorted(myList, key = lambda y: (y[0].lower(), y[1])) 只能对整个列表进行排序命令
                您可以尝试这些并检查发生了什么

                Two keys will be used when we need to sort a list with two constraints one in ascending order and other in descending in the same list or any
                In your example
                sortedList = sorted(myList, key = lambda y: (y[0].lower(), y[1])) can sort entire list only in one order
                you can try these and check whats happening

                sortedList = sorted(myList, key = lambda y: (y[0].lower(), -y[1]))
                sortedList = sorted(myList, key = lambda y: (-y[0].lower(), y[1]))
                sortedList = sorted(myList, key = lambda y: (-y[0].lower(), -y[1]))
                

                希望你在这之后会明白;)

                hope you will understand after this ;)

                这篇关于使用两个键对 Python 列表进行排序,但只有一个键以相反的顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:解压元组的pythonic方法是什么? 下一篇:为什么 python 字符串和元组是不可变的?

                相关文章

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

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

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

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

                  <tfoot id='o0IhI'></tfoot>