更好的编写Python代码的方式
检查Tuple里的每个元素
假设有一个Tuple,里面包含了几个元素:
p = (170, 0.1, 0.6) if p[1] >= 0.5: print u'好深的' if p[2] >= 0.5: print u'好亮啊'
这段代码本身没有任何问题,但是写的时候需要记住Tuple里每个元素都是什么,才能打印出对的描述。为了让代码更容易看懂:
from collections import namedtuple Color = namedtuple('Color', ['hue', 'saturation', 'luminosity']) p = Color(170, 0.1, 0.6) if p['saturation'] >= 0.5: print u'好深的' if p['luminosity'] >= 0.5: print u'好亮啊'
计算列表里的重复元素
假设有一个叫做颜色的列表, 需要计算出这个列表里每个颜色的名字被重复了几次
colors = ['red', 'green', 'red', 'blue', 'green', 'red'] d = {}
一般书写方式:
for color in colors: if color not in d: d[color] = 0 d[color] += 1
稍好一点的写法:
for color in colors: d[color] = d.get(color, 0) + 1
最好的写法:
from collections import defaultdict d = defaultdict(int) for color in colors: d[color] += 1
将一个字典里的内容归类
有一个列表,需要将列表中的内容根据长度归类
names = ['raymond', 'rachel', 'matthew', 'roger', 'bettry', 'melissa', 'judith', 'charlie']
一般写法:
d = {} for name in names: key = len(name) if key not in d: d[key] = [] d[key].append(name)
稍好一点的写法:
for name in names: key = len(name) d.setdefault(key, []).append(name)
最好的写法:
d = defaultdict(list) for name in names: key = len(name) d[key].append(name)
使用Keyword Argument
tw('@obama', False, 20, True)
如果不看ts函数的内容的话,是无法理解这个函数是干什么用的,如果改写成这样呢:
twitter_search('@obama', retweets=False, numtweets=20, popular=True)
同时更新多个变量
编程的时候经常会碰到这种情况,需要用一个临时的变量来存住一个数值,然后过一会再把这个数值取出来
t = y y = x + y x = t
最好的写法:
x, y = y, x+y
所有等号右侧的数值都是旧的数值。这个写法的好处是不需要像原来那样担心每一行顺序的问题。
对序列起始位置做改动
当改动序列第一位的元素时,经常会引起程序速度变慢
names = ['raymond', 'rachel', 'matthew', 'roger', 'bettry', 'melissa', 'judith', 'charlie'] #以下任意操作都会很慢 del names[0] names.pop(0) names.insert(0, 'mark')
最好的方式:
from collections import deque #将names变为可以在左右两端添加或删减的数据类型 names = deque(['raymond', 'rachel', 'matthew', 'roger', 'bettry', 'melissa', 'judith', 'charlie'])
引自:
https://www.youtube.com/watch?v=OSGv2VnC0go
https://www.youtube.com/watch?v=wf-BqAjZb8M

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
tf.Graph().as_default
as_default() method of tensorflow.python.framework.ops.Graph instance Returns a context manager that makes this `Graph` the default graph. This method should be used if you want to create multiple graphs in the same process. For convenience, a global default graph is provided, and all ops will be added to this graph if you do not create a new graph explicitly. Use this method with the `with` keyword to specify that ops created within the scope of a block should be added to this g...
- 下一篇
tf.Graph().get_tensor_by_name
get_tensor_by_name(name) method of tensorflow.python.framework.ops.Graph instance Returns the `Tensor` with the given `name`. This method may be called concurrently from multiple threads. Args: name: The name of the `Tensor` to return. Returns: The `Tensor` with the given `name`. Raises: TypeError: If `name` is not a string. KeyError: If `name` does not correspond to a tensor in this graph.
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7安装Docker,走上虚拟化容器引擎之路
- MySQL8.0.19开启GTID主从同步CentOS8
- Red5直播服务器,属于Java语言的直播服务器
- Mario游戏-低调大师作品
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池