简体   繁体   English

我如何按升序对元组列表进行排序,例如 my_list = [(6,4), (3,4)] 以生成 (3,4) (4,6)

[英]How do i sort my list of tuples in ascending order e.g. my_list = [(6,4), (3,4)] to produce (3,4) (4,6)

How can i sort the below list of tuples to produce tuples of (3,4) (4,6)我如何对下面的元组列表进行排序以生成 (3,4) (4,6) 的元组

my_list = [(6,4), (3,4)]

I have tried the following我尝试了以下

items= [(3,4),(6,4)]
sorted_items= sorted(items)
print(sorted_items)

and

my_list = [(6,4), (3,4)]
my_list.sort(key=lambda tup: (tup[0], tup[1]), reverse=False)
print(my_list)

Thanks谢谢

You can use two calls to sorted() to generate the desired output:您可以使用两次调用sorted()来生成所需的 output:

sorted(tuple(sorted(tup)) for tup in my_list)

This outputs:这输出:

[(3, 4), (4, 6)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 my_list *= 2 和 my_list = my_list * 2 在将 my_list 作为参数的 function 中产生不同的结果? - Why do my_list *= 2 and my_list = my_list * 2 produce different results in a function that takes my_list as an argument? 我应该避免重新打包多个未打包的迭代吗? 例如 (*(1,2),*(3,4)) 得到 (1,2,3,4) - Should I avoid repack multiple iterables unpacked? e.g. (*(1,2),*(3,4)) to get (1,2,3,4) Python:`del my_list` vs`del [my_list]` - Python: `del my_list` vs `del[my_list]` cdef在Cython中列出my_list - cdef list my_list in Cython 如何使列表列表中的每个列表按升序排序? - How do I make each list in the list of lists sort in ascending order? 一种被遗忘的将列表列表转换为元组列表的简单方法:[[1,2], [3,4]] -> [(1,2), (3,4)] - a forgotten simple way to convert list of list to list of tuple: [[1,2], [3,4]] -> [(1,2), (3,4)] 根据索引 1 处的值按升序对元组列表进行排序 - Sort list of tuples in ascending order based on value at index 1 如何按升序对目录列表进行排序? - How Can I Sort a List of Directories By Ascending Order? 按升序排序元组的列表 - Ordering list of tuples by ascending order 如何使用特定列按升序对列表进行排序,而不使用 function 中的构建? (Python) - How do I sort a list in ascending order using a specific column, without using a build in function? (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM