您好,欢迎访问一九零五行业门户网

10个易被忽视但应掌握的Python基本用法

我一辈子都在写代码,但从来没有掌握编码的精髓。大部分情况下使用visual basic,因为我用vb最舒服。同时还略微了解一点其他语言(r、c、javascript、applescript、hypertext和1979年学习的basic)。几年前,我决定只用python,以此来提高我的编码能力。在此过程中重复发明了许多轮子,但我并不介意,因为我享受解决问题的乐趣。同时有时能发现更有效、python式的解决方案。时间长了以后,会有顿悟的时刻,意识到根本没必要用困难且冗长的方式处理问题。下面列出10条python用法,如果我早点发现,也许能节省很多时间。
这里没有列表推导和lambda函数。虽然这两个用法都是python式的,效率高也非常酷,但由于经常在stackoverflow或其他地方碰到,所以学python的应该都知道这两个东西。同时也没有三元运算符、装饰器和生成器,因为我很少用到。
本文还有一个ipython notebook nbviewer版本。
1. 在python 2中使用python 3式的输出
python 2与python 3不兼容,这让我不知道该选择哪个版本的python。最终我选择了python 2,因为当时许多我需要用的库都与python 3不兼容。
但实际上,日常使用中最大的版本差异是输出(print)和除法行为。现在我在python 2的代码中都用import from future来导入python 3的输出和除法。现在我用到的几乎所有库都支持python 3,因此会很快迁移到python 3中。
mynumber = 5 print python 2:print the number is %d % (mynumber)print mynumber / 2,print mynumber // 2 from __future__ import print_functionfrom __future__ import division print('npython 3:')print(the number is {}.format(mynumber))print(mynumber / 2, end=' ')print(mynumber // 2) python 2:the number is 52 2 python 3:the number is 52.5 2
对了,对于c系的那些更喜欢括号而不是缩进的开发者,这里还有一个彩蛋:
from __future__ import bracesfile , line 1from __future__ import bracessyntaxerror: not a chance
2. enumerate(list)
很明显,迭代列表时,应该同时迭代其中的元素及其索引,但在很长一段时间内,我都尴尬的使用计数变量或切片。
mylist = [it's, 'only', 'a', 'model'] for index, item in enumerate(mylist): print(index, item) 0 it's1 only2 a3 model
3. 链式比较操作符
由于我以前使用的是静态语言(在这些语言中该用法有二义性),从来没有将两个比较操作符放在一个表达式中。在许多语言中,4 > 3 > 2会返回false,因为4 > 3的结果是布尔值,而true > 2将得出false。
mynumber = 3 if 4 > mynumber > 2: print(chained comparison operators work! n * 3) chained comparison operators work!chained comparison operators work!chained comparison operators work!
4. collections.counter
python的集合库看上去是最好的。在计算需要集合中元素的个数时,stackoverflow找到的答案是创建有序字典,但我坚持使用一个代码片段来创建字典,计算结果中元素出现的频率。直到有一天,我发现可以用collections.deque。
from collections import counterfrom random import randrangeimport pprint mycounter = counter() for i in range(100): random_number = randrange(10) mycounter[random_number] += 1 for i in range(10): print(i, mycounter[i])
0 101 102 133 64 65 116 107 148 129 8
5. 字典推导
python开发者的一个重要标志就是理解列表推导,但最终我发现字典推导也很有用,特别是在交换字典的键和值的时候。
my_phrase = [no, one, expects, the, spanish, inquisition]my_dict = {key: value for value, key in enumerate(my_phrase)}print(my_dict)reversed_dict = {value: key for key, value in my_dict.items()}print(reversed_dict)
{'inquisition': 5, 'no': 0, 'expects': 2, 'one': 1, 'spanish': 4, 'the': 3}{0: 'no', 1: 'one', 2: 'expects', 3: 'the', 4: 'spanish', 5: 'inquisition'}
6. 用subprocess执行shell命令
以前,我使用os库调用外部命令处理文件,而现在我可以在python中以编码的方式执行诸如ffmpeg这样的复杂命令进行视频编辑。
(是的,我和我的客户都使用windows,如果你们因此鄙视我,我会大方地接受!)
注意,用os库完成这个特定命令比用subprocess更好。我只想有一个大家都熟悉的命令。同时,一般来说,在subprocess中使用shell=true参数是非常糟糕的主意,在这里使用这个参数仅仅是为了能在一个ipython notebook单元中放置命令的输出。不要自己使用这个参数!
import subprocessoutput = subprocess.check_output('dir', shell=true)print(output)
volume in drive c is osvolume serial number is [redacted]directory of c:usersdaviddocuments[redacted] 2014-11-26 06:04 am .2014-11-26 06:04 am ..2014-11-23 11:47 am .git2014-11-26 06:06 am .ipynb_checkpoints2014-11-23 08:59 am cccma2014-09-03 06:58 am 19,450 colorbrewdict.py2014-09-03 06:58 am 92,175 imagecompare.ipynb2014-11-23 08:41 am japan_earthquakes2014-09-03 06:58 am 1,100 license2014-09-03 06:58 am 5,263 monty_monte.ipynb2014-09-03 06:58 am 31,082 pocket_tumblr_reddit_api.ipynb2014-11-26 06:04 am 3,211 readme.md2014-11-26 06:14 am 19,898 top_10_python_idioms.ipynb2014-09-03 06:58 am 5,813 tree_convert_mega_to_gexf.ipynb2014-09-03 06:58 am 5,453 tree_convert_mega_to_json.ipynb2014-09-03 06:58 am 1,211 tree_convert_newick_to_json.py2014-09-03 06:58 am 55,970 weather_ml.ipynb 11 file(s) 240,626 bytes 6 dir(s) 180,880,490,496 bytes free
7. 字典的.get()和.iteritems()方法
字典的get()方法可以设置默认值,当用get()查找的键不存在时,返回方法中的默认值参数是很有用的。与列表中的enumerate()相同,可以用键值元组迭代字典中的元素。
my_dict = {'name': 'lancelot', 'quest': 'holy grail', 'favourite_color': 'blue'} print(my_dict.get('airspeed velocity of an unladen swallow', 'african or european?n')) for key, value in my_dict.iteritems(): print(key, value, sep=: ) african or european? quest: holy grailname: lancelotfavourite_color: blue
8. 用于交换元素的元组解包
在vb中,每当需要交换两个变量时,都要用要一个愚蠢的临时变量:c = a; a = b; b = c
a = 'spam'b = 'eggs' print(a, b) a, b = b, a print(a, b) spam eggseggs spam
9. 内省工具introspection tools
我知道dir()方法,我本以为help()方法和ipython中的?魔法命令是一样的,但help()的功能更强大。
my_dict = {'that': 'an ex-parrot!'} help(my_dict)
help on dict object: class dict(object) | dict() -> new empty dictionary | dict(mapping) -> new dictionary initialized from a mapping object's | (key, value) pairs | dict(iterable) -> new dictionary initialized as if via: | d = {} | for k, v in iterable: | d[k] = v | dict(**kwargs) -> new dictionary initialized with the name=value pairs | in the keyword argument list. for example: dict(one=1, two=2) | | methods defined here: | | __cmp__(...) | x.__cmp__(y) cmp(x,y) | | __contains__(...) | d.__contains__(k) -> true if d has a key k, else false | | __delitem__(...) | x.__delitem__(y) del x[y] | | __eq__(...) | x.__eq__(y) x==y | [truncated for space] | | update(...) | d.update([e, ]**f) -> none. update d from dict/iterable e and f. | if e present and has a .keys() method, does: for k in e: d[k] = e[k] | if e present and lacks .keys() method, does: for (k, v) in e: d[k] = v | in either case, this is followed by: for k in f: d[k] = f[k] | | values(...) | d.values() -> list of d's values | | viewitems(...) | d.viewitems() -> a set-like object providing a view on d's items | | viewkeys(...) | d.viewkeys() -> a set-like object providing a view on d's keys | | viewvalues(...) | d.viewvalues() -> an object providing a view on d's values | | ---------------------------------------------------------------------- | data and other attributes defined here: | | __hash__ = none | | __new__ = | t.__new__(s, ...) -> a new object with type s, a subtype of t
10. pep-8兼容的字符串连接
pep8是python编码样式指南。撇开其他的不看,pep8要求每行不能超过80个字符,超过的部分要换行并缩进。
可以通过反斜杠、带逗号“,”的圆括号“()”、或者额外的加号“+”来完成换行。但对于多行字符串,这些解决方案都不够优雅。python有个多行字符串记号,即三个引号,但这样无法换行后保持缩进。
还有一个方法,那就是不带逗号的圆括号。我不知道为什么这种方式能工作,但能用就行。
my_long_text = (we are no longer the knights who say ni! we are now the knights who say ekki-ekki- ekki-p'tang-zoom-boing-z'nourrwringmm!)print(my_long_text)
we are no longer the knights who say ni! we are now the knights who say ekki-ekki-ekki-p'tang-zoom-boing-z'nourrwringmm!
其它类似信息

推荐信息