3 个超有用的 Python 编程小技巧
作者:迟业 时间:2022-12-02 20:32:03
目录
1、如何按照字典的值的大小进行排序
2、优雅的一次性判断多个条件
3、如何优雅的合并两个字典
1、如何按照字典的值的大小进行排序
我们知道,字典的本质是哈希表,本身是无法排序的,但 Python 3.6 之后,字典是可以按照插入的顺序进行遍历的,这就是有序字典,其中的原理,可以阅读 Python3.6 之后字典是有序的? 。
知道了这一点,就好办了,先把字典的键值对列表排序,然后重新插入新的字典,这样新字典就可以按照值的大小进行遍历输出。
代码如下:
>>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
>>> for k,v in xs.items():#遍历字典
... print(k,v)
...
a 4
b 3
c 2
d 1
>>> new_order = sorted(xs.items(), key=lambda x: x[1]) #对字典的键值对列表排序
>>> new_xs = { k : v for k,v in new_order} #有序列表插入新的字典
>>> new_xs
{'d': 1, 'c': 2, 'b': 3, 'a': 4}
>>> for k,v in new_xs.items(): ##新字典的输出就是有序的
... print(k,v)
...
d 1
c 2
b 3
a 4
对列表的排序,你还可以使用如下方法:
>>> import operator
>>> sorted(xs.items(), key=operator.itemgetter(1))
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
2、优雅的一次性判断多个条件
假如有三个条件,只要有一个为真就可以通过,也许你会这么写:
x, y, z = 0, 1, 0
if x == 1 or y == 1 or z == 1:
print('passed')
实际上,以下三种方法更加 Pythonic
if 1 in (x, y, z):
print('passed')
if x or y or z:
print('passed')
if any((x, y, z)):
print('passed')
最后一个用到了 Python
内置的方法 any()
,any
接受一个可迭代对象作为参数,比如列表或元组,只要其中一个为真,则 any() 方法返回真,
用法示例如下:
>>> any(['a',(2,4),3,True])
True
>>> any(['a',(2,4),3,False])
True
>>> any(['a',(),3,False])
True
>>> any(['',(),0,False])
False
>>> any(('a',(),3,False))
True
>>> any(('',(),0,False))
False
## 注意空的可迭代对象返回 False
>>> any(())
False
>>> any([])
False
>>> any('')
False
>>> any({})
False
与 any()
对应的,就是方法 all()
,只有全部为真,才为真,注意空的可迭代对象一直返回真。
>>> all(['a',(2,4),1,True]) //list都为"真"
True
>>> all(['a',(),1,True]) //list元素中有空tuple
False
>>> all(['a',(2,4),0,True])
False
>>> all(['a',(2,4),3,False])
False
## 注意空的可迭代对象返回 True
>>>all([])
True
>>> all(())
True
>>> all({})
True
>>> all('')
True
查看帮助文档,可以在解释器输入 help:
>>> help(all)
Help on built-in function all in module __builtin__:
all(...)
all(iterable) -> bool
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
3、如何优雅的合并两个字典
操作符可以解包字典,这在合并字典时非常有用,比如:
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 3, 'c': 4}
>>> z = {**x, **y}
>>> z
{'c': 4, 'a': 1, 'b': 3}
如果在 Python2.x 中,需要这么做:
>>> z = dict(x, **y)
>>> z
{'a': 1, 'c': 4, 'b': 3}
来源:https://juejin.cn/post/7039643600799399972
标签:Python,编程,小技巧
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WEB2.0网页制作标准教程(9)第一个CSS布局实例
2008-02-19 19:05:00
![](https://img.aspxhome.com/file/UploadPic/20082/19/2008219191629508s.gif)
python中Pyqt5使用Qlabel标签进行视频播放
2021-12-19 13:32:20
![](https://img.aspxhome.com/file/2023/0/97530_0s.webp)
Python 数据清洗删除缺失值替换缺失值详情
2022-03-02 05:46:11
![](https://img.aspxhome.com/file/2023/7/83927_0s.png)
python 安装库几种方法之cmd,anaconda,pycharm详解
2021-05-23 14:19:58
![](https://img.aspxhome.com/file/2023/8/70888_0s.png)
详解Vue开发网站seo优化方法
2024-04-10 13:47:53
![](https://img.aspxhome.com/file/2023/9/139689_0s.png)
Python在后台自动解压各种压缩文件的实现方法
2022-10-04 17:59:59
Python中处理无效数据的详细教程
2021-11-18 06:11:07
![](https://img.aspxhome.com/file/2023/6/83736_0s.png)
Django如何简单快速实现PUT、DELETE方法
2021-04-14 17:53:07
Golang 中 omitempty的作用
2024-04-25 15:13:03
![](https://img.aspxhome.com/file/2023/4/134154_0s.webp)
SQL Server口令 sql server安全性第1/2页
2024-01-22 19:00:15
python实现按键精灵找色点击功能教程,使用pywin32和Pillow库
2023-11-08 18:30:34
![](https://img.aspxhome.com/file/2023/5/63915_0s.jpg)
Python计算矩阵的和积的实例详解
2021-06-06 10:09:34
结合Python的SimpleHTTPServer源码来解析socket通信
2021-09-05 23:30:27
![](https://img.aspxhome.com/file/2023/6/105046_0s.jpg)
MySQL如何为表和字段取别名详解
2024-01-14 11:35:02
![](https://img.aspxhome.com/file/2023/9/71809_0s.jpg)
SQL的Join使用图解教程
2012-08-21 10:47:23
python基础教程之序列详解
2021-05-29 23:52:04
Python面向对象编程(三)
2021-10-09 18:28:06
python安装模块如何通过setup.py安装(超简单)
2023-06-13 00:46:30
![](https://img.aspxhome.com/file/2023/4/59294_0s.png)
python中使用PIL制作并验证图片验证码
2023-06-05 11:28:33
Python利用FlashText算法实现替换字符串
2023-05-17 06:18:11
![](https://img.aspxhome.com/file/2023/3/82563_0s.png)