1. <legend id='XSd98'><style id='XSd98'><dir id='XSd98'><q id='XSd98'></q></dir></style></legend>

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

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

    2. <tfoot id='XSd98'></tfoot>

      如何按元组的第一个元素对元组列表进行排序?

      时间:2023-08-31
      <tfoot id='VUnGS'></tfoot>
    3. <legend id='VUnGS'><style id='VUnGS'><dir id='VUnGS'><q id='VUnGS'></q></dir></style></legend>

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

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

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

                本文介绍了如何按元组的第一个元素对元组列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个元组列表:

                self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.)
                print self.gridKeys
                

                self.gridKeys:

                self.gridKeys:

                [(7, 3), (6, 9), (0, 7), (1, 6), (3, 7), (2, 5), (8, 5), (5, 8), (4, 0), (9, 0), (6, 7), (5, 5), (7, 6), (0, 4), (1, 1), (3, 2), (2, 6), (8, 2), (4, 5), (9, 3), (6, 0), (7, 5), (0, 1), (3, 1), (9, 9), (7, 8), (2, 1), (8, 9), (9, 4), (5, 1), (7, 2), (1, 5), (3, 6), (2, 2), (8, 6), (4, 1), (9, 7), (6, 4), (5, 4), (7, 1), (0, 5), (1, 0), (0, 8), (3, 5), (2, 7), (8, 3), (4, 6), (9, 2), (6, 1), (5, 7), (7, 4), (0, 2), (1, 3), (4, 8), (3, 0), (2, 8), (9, 8), (8, 0), (6, 2), (5, 0), (1, 4), (3, 9), (2, 3), (1, 9), (8, 7), (4, 2), (9, 6), (6, 5), (5, 3), (7, 0), (6, 8), (0, 6), (1, 7), (0, 9), (3, 4), (2, 4), (8, 4), (5, 9), (4, 7), (9, 1), (6, 6), (5, 6), (7, 7), (0, 3), (1, 2), (4, 9), (3, 3), (2, 9), (8, 1), (4, 4), (6, 3), (0, 0), (7, 9), (3, 8), (2, 0), (1, 8), (8, 8), (4, 3), (9, 5), (5, 2)]
                

                排序后:

                self.gridKeys = self.gridMap.keys() # The keys of the instance of the GridMap (It returns the product of every possible combination of positions in the specified grid, in tuples.)
                self.gridKeys.sort() # They're dicts, so they need to be properly ordered for further XML-analysis.
                print self.gridKeys
                

                self.gridKeys:

                self.gridKeys:

                [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)]
                

                每个元组的第一个元素是x",第二个元素是y".我正在通过迭代移动列表中的对象并使用这些键(因此,如果我想在 x 轴上移动某些东西,我必须遍历所有列,这可能会导致我不是能够解决).

                The first element of each tuple is the "x", and the second the "y". I'm moving objects in a list through iteration and using these keys (So, if I want to move something in the x axis, I have to go through all the column, and that might be causing a horrid problem that I'm not being able to solve).

                如何以这种方式对元组进行排序?:

                How can I sort the tuples in this way?:

                [(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), ...]
                

                推荐答案

                您可以使用 sort 函数的 key 参数对元组进行排序.key 参数的作用是得出一个必须用来比较两个对象的值.所以,在你的情况下,如果你希望 sort 只使用元组中的第一个元素,你可以这样做

                You can use the key parameter of the sort function, to sort the tuples. The function of key parameter, is to come up with a value which has to be used to compare two objects. So, in your case, if you want the sort to use only the first element in the tuple, you can do something like this

                self.gridKeys.sort(key=lambda x: x[0])
                

                如果你只想使用元组中的第二个元素,那么

                If you want to use only the second element in the tuple, then

                self.gridKeys.sort(key=lambda x: x[1])
                

                sort 函数会将列表中的每个元素传递给您作为参数传递给 key 的 lambda 函数,它将使用它返回的值来比较两个列表中的对象.因此,在您的情况下,假设您在列表中有两个这样的项目

                sort function will pass each and every element in the list to the lambda function you pass as parameter to key and it will use the value it returns, to compare two objects in the list. So, in your case, lets say you have two items in the list like this

                data = [(1, 3), (1, 2)]
                

                如果你想按第二个元素排序,那么你会这样做

                and if you want to sort by the second element, then you would do

                data.sort(key=lambda x: x[1])
                

                首先它将 (1, 3) 传递给 lambda 函数,该函数返回索引 1 处的元素,即 3 并将在比较期间表示这个元组.同样的方法,2 将用于第二个元组.

                First it passes (1, 3) to the lambda function which returns the element at index 1, which is 3 and that will represent this tuple during the comparison. The same way, 2 will be used for the second tuple.

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

                上一篇:通过特定索引检查元组列表中是否存在字符串 下一篇:有没有办法检查一个项目是否存在于 Python 元组中?

                相关文章

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

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

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

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