# Python 技巧
来源:互联网 发布:windows平板和安卓平板 编辑:程序博客网 时间:2024/06/18 14:59
# Python 技巧 ##### Github地址:[https://github.com/vimiix/python_tricks](https://github.com/vimiix/python_tricks) ##### Slide链接:[http://vimiix.com/python_tricks](http://vimiix.com/python_tricks) ``` Vimiix 2017.12.20 ``` --- class: inverse ## 多种方法来验证多条件语句 ``` x, y, z = 0, 1, 0 if x == 1 or y == 1 or z == 1: print('passed') if 1 in (x, y, z): print('passed') # These only test for truthiness: if x or y or z: print('passed') if any((x, y, z)): print('passed') ``` --- class: inverse ## 函数参数解包 ``` # Function argument unpacking def myfunc(x, y, z): print(x, y, z) tuple_vec = (1, 0, 1) dict_vec = {'x': 1, 'y': 0, 'z': 1} myfunc(*tuple_vec) # 1, 0, 1 myfunc(**dict_vec) # 1, 0, 1 ``` --- class: inverse ## 使用timeit模块来测试函数执行效率 ``` import timeit rt = timeit.timeit('"-".join(str(n) for n in range(100))', number=10000) print(rt) # 0.252703905106 rt = timeit.timeit('"-".join([str(n) for n in range(100)])', number=10000) print(rt) # 0.234980106354 rt = timeit.timeit('"-".join(map(str, range(100)))', number=10000) print(rt) # 0.152318954468 ``` --- class: inverse ## 合并两个字典在不同版本中的写法 ``` x = {'a': 1, 'b': 2} y = {'b': 3, 'c': 4} # Python 3.5+ z = {**x, **y} print(z) # {'c': 4, 'a': 1, 'b': 3} # Python 2.x z = dict(x, **y) print(z) # {'a': 1, 'c': 4, 'b': 3} ``` --- class: inverse ## 巧用json模块更舒服的打印字典 ``` # 标准打印出来,对于阅读不是很友好 my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee} print(my_mapping) # {'b': 42, 'c': 12648430. 'a': 23} # đŸ˜ž # 使用json可以更好的展示 import json print(json.dumps(my_mapping, indent=4, sort_keys=True)) # { # "a": 23, # "b": 42, # "c": 12648430 # } ``` --- class: inverse ## 通过字典的值(value)排序输出 ``` # How to sort a Python dict by value xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1} print(sorted(xs.items(), key=lambda x: x[1])) # [('d', 1), ('c', 2), ('b', 3), ('a', 4)] # Or: import operator print(sorted(xs.items(), key=operator.itemgetter(1))) # [('d', 1), ('c', 2), ('b', 3), ('a', 4)] ``` --- class: inverse ## 字典的get用法和默认值 ``` name_for_userid = { 382: "Alice", 590: "Bob", 951: "Dilbert", } def greeting(userid): return "Hi %s!" % name_for_userid.get(userid, "there") print(greeting(382)) # "Hi Alice!" print(greeting(2333333)) # "Hi there!" ``` --- class: inverse ## 用namedtuple来代替简单的类 ``` from collections import namedtuple Car = namedtuple('Car', 'color mileage') # 实例化一个Car,并像类一样使用 my_car = Car('red', 3812.4) print(my_car.color) # 'red' print(my_car.mileage) # 3812.4 print(my_car) # Car(color='red' , mileage=3812.4) # 就像元组一样,namedtuple也不可更改: my_car.color = 'blue' # AttributeError: "can't set attribute" ``` --- class: center, middle, inverse ## 持续更新中... --- class: middle, inverse ## 联系方式 ### - Website: http://vimiix.com - Twitter: http://twitter.com/_vimiix - GitHub: http://github.com/vimiix - Email: [i@vimiix.com](mailto:i@vimiix.com)
阅读全文
0 0
- python技巧
- python技巧
- python技巧
- python技巧
- Python 技巧
- python技巧
- python-技巧
- python技巧
- python技巧
- python技巧
- Python技巧
- Python技巧
- Python 技巧
- # Python 技巧
- python技巧31[python Tip2]
- Python技巧-警惕关键字
- Python技巧汇总
- Python技巧 ---- 不断更新中
- 服务端I/O性能大比拼:Node、PHP、Java、Go
- Windows安装Mysql时出现requires .NET Framework4.0
- Android 探究数据库greenDAO(一)
- 使用daemontools监控zookeeper,storm等进程
- 代码谱写传奇,深度揭秘中国开发者现状!
- # Python 技巧
- 网络编程(UDP协议)
- oracle常用函数
- 编译与链接(三)——动态链接
- GreenDao教程(3):一对一,一对多,多对多
- java中switch语句用法详解
- hihocoder#1241 : Best Route in a Grid(DP)
- AS3 外部读取图片
- ECUG Con PPT大放送&《Go Web编程》中奖名单公布