Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想获取我的元组列表,其中每个元组由一个字符串和一个集合组成,然后重新排列它,以便它按与其关联集合中的大多数条目的字符串排序。
关于如何解决这个问题的指针?
将一个键函数传递给list.sort():
list.sort()
my_list = [("a", set([1, 2, 3])), ("b", set([1, 2, 3, 4])), ("c", set([1, 2]))] my_list.sort(key=lambda x: len(x[1]), reverse=True)
这导致my_list被
my_list
[('b', set([1, 2, 3, 4])), ('a', set([1, 2, 3])), ('c', set([1, 2]))]