Pythonic way to sorting list of namedtuples by field name

后端 未结 5 1436
耶瑟儿~
耶瑟儿~ 2020-12-05 01:21

I want to sort a list of named tuples without having to remember the index of the fieldname. My solution seems rather awkward and was hoping someone would have a more elegan

5条回答
  •  感情败类
    2020-12-05 01:55

    This might be a bit too 'magical' for some, but I'm partial to:

    # sort list by name
    print(sorted(seq, key=Person.name.fget))
    

提交回复
热议问题