
python小技巧
RongjianShaw
这个作者很懒,什么都没留下…
展开
-
python, 交换列表中的两个值遇到的坑
python中交换两个值有种很方便的用法,如下a, b = b, a对于两侧都只有2或3个值的时候,它是通过栈stack来操作的>>> import dis>>> def foo(a, b):... a, b = b, a... >>> dis.dis(foo) 2 0 LOAD_FAST ...原创 2020-05-07 14:55:10 · 1033 阅读 · 4 评论 -
python,对列表中某特定位置进行排序
一维情况比如要对alist = [3,1,2,4,5]排序,sort和sorted的用法为alist = [3,1,2,4,5]new_list = sorted(alist)alist.sort()print(new_list)print(alist)结果为[1, 2, 3, 4, 5][1, 2, 3, 4, 5]sort作为列表的方法使用,写成list.sort()s...原创 2020-05-05 13:04:05 · 5565 阅读 · 0 评论