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]))]