Skip to content

Commit

Permalink
sort 嵌套排序技巧
Browse files Browse the repository at this point in the history
  • Loading branch information
PegasusWang committed Jun 3, 2022
1 parent 4eb9fe7 commit 2c8a81b
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/19_python内置常用算法和数据结构/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ class Solution:
# python list 技巧

```py
# 排序嵌套 list
# 排序嵌套 list,比如元素值是一个 tuple 或者 list
l = [('a', 1), ('c', 2), ('b',3)]
sorted(l, key=lambda p:p[0]) # 根据第1个值排序,[('a', 1), ('b', 3), ('c', 2)]
sorted(l, key=lambda p:p[1]) # 根据第2个值排序,[('a', 1), ('c', 2), ('b', 3)]
sorted(l, key=lambda p:(-p[0], p[1])) # 先根据第一个倒排,如果相等再根据第二个正排序

# 同时获取最大值的下标和值
l = [1,2,5,4,3]
Expand Down

0 comments on commit 2c8a81b

Please sign in to comment.