Sorting 使用此表单的元素(';string';number)对列表进行排序
我想在此表单中对列表进行排序:Sorting 使用此表单的元素(';string';number)对列表进行排序,sorting,python-3.x,Sorting,Python 3.x,我想在此表单中对列表进行排序: [('Initial value problem', 0.0), ('Duns Scotus', 0.0), ('Open front unrounded vowel', 0.0), ('Android version history', 0.0001), ('Research ethics', 0.0001), ('Music technology', 0.0), ('Karl Bechert', 0.0001), ('Motion
[('Initial value problem', 0.0),
('Duns Scotus', 0.0),
('Open front unrounded vowel', 0.0),
('Android version history', 0.0001),
('Research ethics', 0.0001),
('Music technology', 0.0),
('Karl Bechert', 0.0001),
('Motion (physics)', 0.0001),
('Karl Friedrich Burdach', 0.0)]
我想根据每个元素中的数字对这个列表进行排序。
谢谢。只需通过设置
键
参数对每个项目的第二个元素排序即可
>>> l = [('Initial value problem', 0.0),
('Duns Scotus', 0.0),
('Open front unrounded vowel', 0.0),
('Android version history', 0.0001),
('Research ethics', 0.0001),
('Music technology', 0.0),
('Karl Bechert', 0.0001),
('Motion (physics)', 0.0001),
('Karl Friedrich Burdach', 0.0)]
>>> print sorted(l, key=lambda x: x[1])
[('Initial value problem', 0.0),
('Duns Scotus', 0.0),
('Open front unrounded vowel', 0.0),
('Music technology', 0.0),
('Karl Friedrich Burdach', 0.0),
('Android version history', 0.0001),
('Research ethics', 0.0001),
('Karl Bechert', 0.0001),
('Motion (physics)', 0.0001)]
如果希望它们按相反的顺序排列,也可以在调用
sorted()
@mins中设置reverse=True
参数。因为一开始我不知道从哪里开始解决这个问题。可能是@mins的副本谢谢你告诉我这个。