3 回答
TA贡献1851条经验 获得超4个赞
如果您想按姓氏排序,这可能会起作用:
a = myList[1:]
a.sort(key=lambda tup: tup[0].split(" ")[1])
myList[1:] = a
结果:
[
('NAME', 'SOME LABEL 1', 'SOME LABEL 2'),
('Adrian Benco', 23, 2),
('John Bowes', 35, 2),
('Jessica Farner', 43, 20),
('Gary Funa', 49, 7),
('Alice Gordon', 48, 7),
('Gina Gundo', 47, 30),
('Adam Hao', 50, 4),
('Greg Hyde', 34, 20),
('Lee Jackson', 49, 7),
('Kevin Lee', 45, 4),
('Gina Marnico', 40, 3),
('Jessica Randolf', 44, 8),
('Katy Sendej', 30, 2),
('George Smith', 8, 3),
('Sam Tuno', 15, 4),
('Ryan Valins', 39, 7)
]
如果你想按姓氏排序,然后首先你可以这样做:
a.sort(key=lambda tup: list(reversed(tup[0].split(" "))))
TA贡献1851条经验 获得超5个赞
删除标签线,它的工作原理:
myList.sort(key=lambda tup: (tup[0].split(" ")[1]))
结果 :
('Adrian Benco', 23, 2)
('John Bowes', 35, 2)
('Jessica Farner', 43, 20)
('Gary Funa', 49, 7)
('Alice Gordon', 48, 7)
('Gina Gundo', 47, 30)
('Adam Hao', 50, 4)
('Greg Hyde', 34, 20)
('Lee Jackson', 49, 7)
('Kevin Lee', 45, 4)
('Gina Marnico', 40, 3)
('Jessica Randolf', 44, 8)
('Katy Sendej', 30, 2)
('George Smith', 8, 3)
('Sam Tuno', 15, 4)
('Ryan Valins', 39, 7)
添加回答
举报