From 5d63cf3332328a0e5fe06eb38e8b01d01006216a Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Tue, 26 Apr 2022 09:31:07 +0800 Subject: [PATCH 01/11] =?UTF-8?q?python=20sys.setrecursionlimit=20?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E6=A0=88=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builtins.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" index 0727bd0..4d00568 100644 --- "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" +++ "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" @@ -42,6 +42,25 @@ - 自定义排序函数。python2 可以用 `nums.sort(cmp=lambda a, b: a - b)`,但是python3移除了cmp参数。 python3如果想要用自定义排序函数可以使用 functools.cmp_to_key 函数改成 `nums.sort(key=cmp_to_key(lambda a, b: a - b))` +# python 递归暴栈(栈溢出) + +python 递归函数默认递归深度比较小,你可以通过 `sys.getrecursionlimit()` 函数打印出来。 +我在 mac 机器上测试的时候,以下结果 python2 输出 1000。这就导致一些递归函数测试用例稍微多一些就会报错。 +(一个用例超过上千个数据就会报错了) + +```py +import sys +print(sys.getrecursionlimit()) # 我的 mac 机器上输出 1000 +``` + +可以把以下代码设置最大栈深度,放到文件开头,在牛客上提交代码的时候可以避免一些递归代码报错。 +(leetcode 似乎给设置了,类似的题目发现力扣上提交不会栈溢出但是在牛客就会) + +```py +import sys +sys.setrecursionlimit(100000) # 设置函数栈深度足够大,避免栈溢出错误 +``` + # python int 值范围 ``` From 5c765440b73d04a70b401e1aa22253432d883d15 Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Wed, 27 Apr 2022 12:34:35 +0800 Subject: [PATCH 02/11] pytyhon matrix sum --- .../builtins.md" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" index 4d00568..5c1acf4 100644 --- "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" +++ "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" @@ -122,6 +122,9 @@ issorted = all(l[i] <= l[i+1] for i in range(len(l) - 1)) # python3 一行代码求前缀和 from itertools import accumulate presums = list(accumulate([1,2,3])) # [1, 3, 6] + +# 一行代码求矩阵和 https://stackoverflow.com/questions/10713150/how-to-sum-a-2d-array-in-python +allsum = sum(map(sum, matrix)) # 或者 allsum = sum((sum(row) for row in matrix)) ``` # 链表题目调试函数 From d6d0c41cadc777f509ff5ef1de885a86c9449b58 Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Wed, 27 Apr 2022 16:28:55 +0800 Subject: [PATCH 03/11] =?UTF-8?q?python=20=E8=B4=9F=E6=95=B0=E4=BD=8D?= =?UTF-8?q?=E8=BF=90=E7=AE=97=E7=9A=84=E5=9D=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builtins.md" | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" index 5c1acf4..659404f 100644 --- "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" +++ "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" @@ -64,7 +64,7 @@ sys.setrecursionlimit(100000) # 设置函数栈深度足够大,避免栈溢出 # python int 值范围 ``` -# 乘方 (比较推荐,py2/3 都兼容不容易出错) +# 乘方 (比较推荐⭐️,py2/3 都兼容不容易出错) MAXINT = 2**63-1 MININT = -2**63 @@ -81,6 +81,23 @@ MAXINT = (1<<63) - 1 MININT = ~MAXINT ``` +# python 负数位运算的坑 +1. Python3 中的整型是补码形式存储的 +2. Python3 中 bin 一个负数(十进制表示),输出的是它的原码的二进制表示加上个负号 +3. 为了获得负数(十进制表示)的补码,需要手动将其和十六进制数 0xffffffff 进行按位与操作,得到结果是个十六进制数,再交给 bin() 进行输出, +得到的才是你想要的补码表示。 + +```py +# 整数转换 https://leetcode-cn.com/problems/convert-integer-lcci/ +class Solution: + def convertInteger(self, A: int, B: int) -> int: + return bin((A & 0xffffffff) ^ (B & 0xffffffff)).count('1') +``` + +参考: +- https://www.runoob.com/w3cnote/python-negative-storage.html +- https://leetcode-cn.com/problems/convert-integer-lcci/solution/python3-zhu-yi-qi-dui-yu-fu-shu-de-cun-chu-fang-sh/ + # python list/dict 排序等技巧 ```py @@ -452,7 +469,7 @@ def gen_tree(vals): return root ``` -# python 交换列表元素的坑 +# python 交换列表元素的坑(交换副作用) ``` # 41. 缺失的第一个正数 https://leetcode-cn.com/problems/first-missing-positive/ @@ -474,7 +491,7 @@ class Solution(object): while 1 <= nums[i] <= n and nums[nums[i]-1] != nums[i]: # NOTE: 注意这一句交换右边有副作用的,不能颠倒!!! # nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i] # 这么写死循环! - nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1] + nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1] # 有副作用的放前边 for i in range(n): if nums[i] != i+1: return i+1 From 9e4c22401bea275b8fa68d74a1862f08f2b0067e Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Wed, 27 Apr 2022 17:22:49 +0800 Subject: [PATCH 04/11] leetcode 260 --- ...1\347\232\204\346\225\260\345\255\227).py" | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git "a/\345\211\221\346\214\207offer/40_NumbersAppearOnce(\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227).py" "b/\345\211\221\346\214\207offer/40_NumbersAppearOnce(\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227).py" index 30ada80..fa6f8fc 100644 --- "a/\345\211\221\346\214\207offer/40_NumbersAppearOnce(\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227).py" +++ "b/\345\211\221\346\214\207offer/40_NumbersAppearOnce(\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227).py" @@ -31,6 +31,7 @@ class Solution1: def singleNumber(self, nums): """ + 类似题:求只出现一次的数字,其他都出现两次。 :type nums: List[int] :rtype: int """ @@ -58,12 +59,45 @@ def get_single_num(nums): while single & mask == 0: mask = mask << 1 - print(mask,'||||||||||||||||||') + print(mask, '||||||||||||||||||') left = [i for i in nums if i & mask] right = [i for i in nums if not(i & mask)] return [get_single_num(left), get_single_num(right)] +class Solution(object): + def singleNumber(self, nums): + """ + 思路:异或。 + + 136 题做过只出现一次的一个数字,本题有两个只出现一次的数字。 + 核心在于把数组分成两个。怎么分组呢? + + 假设只出现一次的数字式 x1,x2,所有元素异或结果是 x。一定有 x=x1^x2。 + x不能是0,否则x1==x2,就不是只出现一次的数字了。 + *可以用位运算 x&-x 取出x 二进制位最低位的 1,设其实是第 L 位置。* + 可以据此分类数组,一组是二进制第 L位置为 0 的数字,一组L 位置为 1的数字。 + x1,x2会分别出现在两个组中。这样第一组全部异或得到x1, 第二组全部异或得到 x2 + + :type nums: List[int] + :rtype: List[int] + """ + x = 0 + for num in nums: + x ^= num + + low1pos = x & -x # 这里也可以用移位运算,x&-x 不太好想,类似上边那个解法 + x1, x2 = 0, 0 + + for num in nums: + if num & low1pos: + x1 ^= num + else: + x2 ^= num + + return x1, x2 + + def test(): s = Solution() assert s.singleNumber([1, 2, 1, 3, 2, 5]) == [3, 5] From 433680bbe517c028fb87c7c0ab6b9a0f62c72ab3 Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Thu, 28 Apr 2022 13:27:24 +0800 Subject: [PATCH 05/11] visualization website --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6e9ab30..0d2a994 100644 --- a/README.md +++ b/README.md @@ -121,9 +121,12 @@ Python 抽象程度比较高, 我们能用更少的代码来实现功能,同 ## 算法可视化 学习算法的过程中有时候会比较抽象,这里给大家推荐一些可视化的网站,方便更直观地理解各种算法和数据结构的执行步骤: +遇到一个算法或数据结构,你可以 google 搜索 "名称+ visualization" 找到一些可视化网站方便理解,比如学习跳跃表的时候笔者就 +可以通过 goole "skip list visualization" 搜到一些可视化网站帮助你理解它的工作原理。 - https://github.com/algorithm-visualizer/algorithm-visualizer - https://www.cs.usfca.edu/~galles/visualization/Algorithms.html +- https://cmps-people.ok.ubc.ca/ylucet/DS/Algorithms.html - https://runestone.academy/runestone/books/published/pythonds/index.html# ## 讲课形式 From 8fc6d7d04e611cb1b8eb0f30f540812a2ec1560b Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Thu, 28 Apr 2022 18:22:34 +0800 Subject: [PATCH 06/11] python list and dict skills --- .../builtins.md" | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" index 659404f..cb57671 100644 --- "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" +++ "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" @@ -98,19 +98,9 @@ class Solution: - https://www.runoob.com/w3cnote/python-negative-storage.html - https://leetcode-cn.com/problems/convert-integer-lcci/solution/python3-zhu-yi-qi-dui-yu-fu-shu-de-cun-chu-fang-sh/ -# python list/dict 排序等技巧 +# python list 技巧 ```py -# python 根据 key,value 排序字典 -d = {'d': 4, 'a': 1, 'b': 2, 'c':3} -# dict sort by key and reverse -dict(sorted(d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4} -dict(sorted(d.items(), reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1} - -# dict sort by value and reverse -dict(sorted(d.items(), key = lambda kv:kv[1])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4} -dict(sorted(d.items(), key = lambda kv:kv[1], reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1} - # 排序嵌套 list l = [('a', 1), ('c', 2), ('b',3)] sorted(l, key=lambda p:p[0]) # 根据第1个值排序,[('a', 1), ('b', 3), ('c', 2)] @@ -120,18 +110,11 @@ sorted(l, key=lambda p:p[1]) # 根据第2个值排序,[('a', 1), ('c', 2), ('b l = [1,2,5,4,3] maxi, maxval = max(enumerate(l), key=lambda iv: iv[1]) # 2, 5 -# 获取字典对应的最大值对应的 key,value -mydict = {'A':4,'B':10,'C':0,'D':87} -maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for minimum. -maxk, maxv = maximum, mydict[maximum] -# 或者 -maxk, maxv = max(mydict.items(), key=lambda k: k[1]) - -# python3 排序list自定义函数(python2 直接用 cmp 参数) +# python3 排序list自定义函数(python2 直接用 cmp 参数, python3 需要用 cmp_to_key 转成 key 参数) from functools import cmp_to_key nums = [3,2,1,4,5] -sorted(nums, key= cmp_to_key(lambda a,b: a-b) ) # [1 ,2 ,3, 4, 5] -sorted(nums, key= cmp_to_key(lambda a,b: b-a) ) # [5, 4, 3, 2, 1] +sorted(nums, key=cmp_to_key(lambda a,b: a-b) ) # [1 ,2 ,3, 4, 5] +sorted(nums, key=cmp_to_key(lambda a,b: b-a) ) # [5, 4, 3, 2, 1] # 一行代码判断列表是否有序 issorted = all(l[i] <= l[i+1] for i in range(len(l) - 1)) @@ -140,10 +123,36 @@ issorted = all(l[i] <= l[i+1] for i in range(len(l) - 1)) from itertools import accumulate presums = list(accumulate([1,2,3])) # [1, 3, 6] -# 一行代码求矩阵和 https://stackoverflow.com/questions/10713150/how-to-sum-a-2d-array-in-python +# 一行代码求矩阵元素总和 https://stackoverflow.com/questions/10713150/how-to-sum-a-2d-array-in-python allsum = sum(map(sum, matrix)) # 或者 allsum = sum((sum(row) for row in matrix)) ``` +# python dict 技巧 + +```py +# python 根据 key,value 排序字典 +d = {'d': 4, 'a': 1, 'b': 2, 'c':3} +# dict sort by **key** and reverse +dict(sorted(d.items())) # {'a': 1, 'b': 2, 'c': 3, 'd': 4} +dict(sorted(d.items(), reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1} + +# dict sort by **value** and reverse +dict(sorted(d.items(), key = lambda kv:kv[1])) # {'a': 1, 'b': 2, 'c': 3, 'd': 4} +dict(sorted(d.items(), key = lambda kv:kv[1], reverse=True)) # {'d': 4, 'c': 3, 'b': 2, 'a': 1} + +# 获取字典对应的最大值对应的 key,value +mydict = {'A':4,'B':10,'C':0,'D':87} +maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for minimum. +maxk, maxv = maximum, mydict[maximum] +# 或者 +maxk, maxv = max(mydict.items(), key=lambda k: k[1]) + +# 支持默认值的有序字典 (OrderedDict and defaultdict) +# https://stackoverflow.com/questions/6190331/how-to-implement-an-ordered-default-dict +od = OrderedDict() # collections.OrderedDict() +od[i] = od.get(i, 0) + 1 # 间接实现了 defaultdict(int) ,同时保持了插入字典的 key 顺序 +``` + # 链表题目调试函数 ```py From 10074160050ae47a884eef7332a2fc2ea41a61ff Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Thu, 28 Apr 2022 18:25:04 +0800 Subject: [PATCH 07/11] =?UTF-8?q?OrderedDict=20=E6=8F=92=E5=85=A5=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builtins.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" index cb57671..be78927 100644 --- "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" +++ "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" @@ -147,7 +147,7 @@ maxk, maxv = maximum, mydict[maximum] # 或者 maxk, maxv = max(mydict.items(), key=lambda k: k[1]) -# 支持默认值的有序字典 (OrderedDict and defaultdict) +# 支持默认值的有序字典 (OrderedDict and defaultdict) (注意是 key 插入顺序不是字典序) # https://stackoverflow.com/questions/6190331/how-to-implement-an-ordered-default-dict od = OrderedDict() # collections.OrderedDict() od[i] = od.get(i, 0) + 1 # 间接实现了 defaultdict(int) ,同时保持了插入字典的 key 顺序 From 9bf19c59a7cf95b0c683f9b16d671e92cc3fa5d7 Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Thu, 5 May 2022 11:47:19 +0800 Subject: [PATCH 08/11] lru comment --- "docs/03_\351\223\276\350\241\250/lru_cache.py" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/docs/03_\351\223\276\350\241\250/lru_cache.py" "b/docs/03_\351\223\276\350\241\250/lru_cache.py" index 0612a66..e1683bb 100644 --- "a/docs/03_\351\223\276\350\241\250/lru_cache.py" +++ "b/docs/03_\351\223\276\350\241\250/lru_cache.py" @@ -197,7 +197,7 @@ def put(self, key, value): :type value: int :rtype: None """ - if key in self.map: + if key in self.map: # 更新不会改变元素个数,这里不用判断是否需要剔除 node = self.map[key] node.value = value # 修改结构体会也会修改 map 对应 value 的引用 self.ll.delete_node(node) From 4eb9fe797f8d9e38df30c9352819e7dec37b970a Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Sun, 15 May 2022 16:42:45 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E4=B8=80=E8=A1=8C=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=85=83=E7=B4=A0=E6=98=AF=E5=90=A6=E5=9C=A8?= =?UTF-8?q?=E7=9F=A9=E9=98=B5=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builtins.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" index be78927..53c89dc 100644 --- "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" +++ "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" @@ -125,6 +125,8 @@ presums = list(accumulate([1,2,3])) # [1, 3, 6] # 一行代码求矩阵元素总和 https://stackoverflow.com/questions/10713150/how-to-sum-a-2d-array-in-python allsum = sum(map(sum, matrix)) # 或者 allsum = sum((sum(row) for row in matrix)) +# 一行代码判断一个元素是否在矩阵中,比如判断 1 是否在矩阵matrix中 +any(1 in row for row in matrix) ``` # python dict 技巧 From 2c8a81b284f78964ebb7a7e83abf920067928f33 Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Fri, 3 Jun 2022 15:28:23 +0800 Subject: [PATCH 10/11] =?UTF-8?q?sort=20=E5=B5=8C=E5=A5=97=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E6=8A=80=E5=B7=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builtins.md" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" index 53c89dc..d9f0aba 100644 --- "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" +++ "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" @@ -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] From 3469a79c34b6c08ae52797c3974b49fbfa8cca51 Mon Sep 17 00:00:00 2001 From: PegasusWang <291374108@qq.com> Date: Mon, 13 Jun 2022 09:53:41 +0800 Subject: [PATCH 11/11] one line get max matrix val --- .../builtins.md" | 2 ++ 1 file changed, 2 insertions(+) diff --git "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" index d9f0aba..a38b5ef 100644 --- "a/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" +++ "b/docs/19_python\345\206\205\347\275\256\345\270\270\347\224\250\347\256\227\346\263\225\345\222\214\346\225\260\346\215\256\347\273\223\346\236\204/builtins.md" @@ -128,6 +128,8 @@ presums = list(accumulate([1,2,3])) # [1, 3, 6] allsum = sum(map(sum, matrix)) # 或者 allsum = sum((sum(row) for row in matrix)) # 一行代码判断一个元素是否在矩阵中,比如判断 1 是否在矩阵matrix中 any(1 in row for row in matrix) +# 一行代码获取矩阵最大、最小值 +maxval = max(map(max, matrix)) ``` # python dict 技巧