>>> sorted([1,-12,13,-4],key=abs)
[1, -4, -12, 13]

2.字符串排序

按ASCII排序

默认情况下,对字符串排序,是按照ASCII的大小比较的,由于'Z' < 'a',结果,大写字母Z会排在小写字母a的前面。

>>> sorted(['bob', 'about', 'Zoo', 'Credit'])
['Credit', 'Zoo', 'about', 'bob']

按a-z排序

 

>>> sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
['about', 'bob', 'Credit', 'Zoo']

 

 

按z-a排序

>>> sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=True)
['Zoo', 'Credit', 'bob', 'about']

 

相关文章:

  • 2021-07-18
  • 2022-02-09
  • 2021-05-21
猜你喜欢
  • 2021-11-01
  • 2021-06-02
  • 2021-05-22
  • 2021-12-01
  • 2022-01-19
  • 2021-08-17
  • 2021-07-29
相关资源
相似解决方案