[英]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.