问题:
I have some data either in a list of lists or a list of tuples, like this: 我在列表列表或元组列表中都有一些数据,如下所示:
data = [[1,2,3], [4,5,6], [7,8,9]]
data = [(1,2,3), (4,5,6), (7,8,9)]
And I want to sort by the 2nd element in the subset. 我想按子集中的第二个元素排序。 Meaning, sorting by 2,5,8 where 2
is from (1,2,3)
, 5
is from (4,5,6)
. 意思是,按2,5,8排序,其中2
来自(1,2,3)
, 5
来自(4,5,6)
。 What is the common way to do this? 常见的做法是什么? Should I store tuples or lists in my list? 我应该在列表中存储元组或列表吗?
解决方案:
参考一: https://stackoom.com/question/D6AV/如何按给定索引处的元素对列表-元组进行排序-列表-元组参考二: https://oldbug.net/q/D6AV/How-to-sort-list-tuple-of-lists-tuples-by-the-element-at-a-given-index
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4405597