【问题标题】:sorting a list of tuples with names [duplicate]对具有名称的元组列表进行排序[重复]
【发布时间】:2018-06-03 20:28:49
【问题描述】:

我有一个看起来像这样的列表,

[('1', 'Michael'),
('2', 'Christopher'),
('3', 'Matthew'),
('4', 'Joshua'),
('5', 'Daniel'),
('6', 'David'),
('7', 'Andrew'),
('8', 'James'),
('9', 'Justin'),
('10', 'Joseph'),
('11', 'Ryan'),
('12', 'John'),
('13', 'Robert'),
('14', 'Nicholas'),]

我正在尝试使用第二个元素(即名称)对其进行排序,我不确定如何执行此操作, 这是我尝试过的,但我无法让它工作,

sorted(mod , key = lambda x:mod[1])
sorted(mod, key = [x for x,v in enumerate(mod) mod[x][1]]

任何建议都会有所帮助,在此先感谢。

【问题讨论】:

标签: python


【解决方案1】:
l= [('1', 'Michael'),
('2', 'Christopher'),
('3', 'Matthew'),
('4', 'Joshua'),
('5', 'Daniel'),
('6', 'David'),
('7', 'Andrew'),
('8', 'James'),
('9', 'Justin'),
('10', 'Joseph'),
('11', 'Ryan'),
('12', 'John'),
('13', 'Robert'),
('14', 'Nicholas'),]

result = sorted(l,key=lambda t:t[1])
print(result)

【讨论】:

  • 非常感谢您的宝贵时间
  • 不客气 :-)
【解决方案2】:

sorted(mod , key = lambda x:x[1])

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2014-07-18
    • 2015-01-28
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多