<legend id='pz1pU'><style id='pz1pU'><dir id='pz1pU'><q id='pz1pU'></q></dir></style></legend>
  • <tfoot id='pz1pU'></tfoot>

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

      1. <small id='pz1pU'></small><noframes id='pz1pU'>

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

        使用自定义键对python中的元组进行排序

        sorting tuples in python with a custom key(使用自定义键对python中的元组进行排序)
          • <small id='f4iIF'></small><noframes id='f4iIF'>

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

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

                <legend id='f4iIF'><style id='f4iIF'><dir id='f4iIF'><q id='f4iIF'></q></dir></style></legend>
                  <tbody id='f4iIF'></tbody>
                <tfoot id='f4iIF'></tfoot>

                  本文介绍了使用自定义键对python中的元组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  嗨:我正在尝试以自定义方式对元组列表进行排序:
                  例如:

                  lt = [(2,4), (4,5), (5,2)]

                  必须排序:

                  lt = [(5,2), (2,4), (4,5)]

                  规则:
                  * 如果 a[1] == b[0]
                  ,则 b 元组大于元组* 如果 a[0] == b[1] 则 a 元组大于 b 元组

                  我已经实现了一个这样的 cmp 函数:

                  def tcmp(a, b):如果 a[1] == b[0]:返回 -1elif a[0] == b[1]:返回 1别的:返回 0

                  但对列表进行排序:

                  lt.sort(tcmp)

                  给我看:

                  lt = [(2, 4), (4, 5), (5, 2)]

                  我做错了什么?

                  解决方案

                  我不确定您的比较函数在数学意义上是否有效,即传递性.给定 a, b, c 一个比较函数,表示 a >bb >c 意味着 a >c.排序过程依赖于这个属性.

                  更不用说根据你的规则,对于 a = [1, 2]b = [2, 1] 你有两个 a[1] == b[0]a[0] == b[1] 这意味着 a 既大于也小于 b.p>

                  Hi: I'm trying to sort a list of tuples in a custom way:
                  For example:

                  lt = [(2,4), (4,5), (5,2)]
                  

                  must be sorted:

                  lt = [(5,2), (2,4), (4,5)]
                  

                  Rules:
                  * b tuple is greater than a tuple if a[1] == b[0]
                  * a tuple is greater than b tuple if a[0] == b[1]

                  I've implemented a cmp function like this:

                  def tcmp(a, b):
                      if a[1] == b[0]:
                         return -1
                      elif a[0] == b[1]:
                         return 1
                      else:
                         return 0
                  

                  but sorting the list:

                  lt.sort(tcmp)
                  

                  lt show me:

                  lt = [(2, 4), (4, 5), (5, 2)]
                  

                  What am I doing wrong?

                  解决方案

                  I'm not sure your comparison function is a valid one in a mathematical sense, i.e. transitive. Given a, b, c a comparison function saying that a > b and b > c implies that a > c. Sorting procedures rely on this property.

                  Not to mention that by your rules, for a = [1, 2] and b = [2, 1] you have both a[1] == b[0] and a[0] == b[1] which means that a is both greater and smaller than b.

                  这篇关于使用自定义键对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 与否?)
                    <bdo id='ovkbu'></bdo><ul id='ovkbu'></ul>

                          <tbody id='ovkbu'></tbody>
                      1. <tfoot id='ovkbu'></tfoot>
                            <legend id='ovkbu'><style id='ovkbu'><dir id='ovkbu'><q id='ovkbu'></q></dir></style></legend>

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

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