Skip to content

Commit b2c3b3a

Browse files
committed
max dict value
1 parent b4f3451 commit b2c3b3a

File tree

1 file changed

+7
-0
lines changed
  • docs/19_python内置常用算法和数据结构

1 file changed

+7
-0
lines changed

docs/19_python内置常用算法和数据结构/builtins.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ sorted(l, key=lambda p:p[1]) # 根据第2个值排序,[('a', 1), ('c', 2), ('b
8383
# 同时获取最大值的下标和值
8484
l = [1,2,5,4,3]
8585
maxi, maxval = max(enumerate(l), key=lambda iv: iv[1]) # 2, 5
86+
87+
# 获取字典对应的最大值对应的 key,value
88+
mydict = {'A':4,'B':10,'C':0,'D':87}
89+
maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for minimum.
90+
maxk, maxv = maximum, mydict[maximum]
91+
# 或者
92+
maxk, maxv = max(mydict.items(), key=lambda k: k[1])
8693
```
8794

8895
# 链表题目调试函数

0 commit comments

Comments
 (0)