值得收藏,Python 开发中的高级技巧

yipeiwu_com6年前Python基础

Python 开发中有哪些高级技巧?这是知乎上一个问题,我总结了一些常见的技巧在这里,可能谈不上多高级,但掌握这些至少可以让你的代码看起来 Pythonic 一点。如果你还在按照类C语言的那套风格来写的话,在 code review 恐怕会要被吐槽了。

列表推导式

>>> chars = [ c for c in 'python' ]
>>> chars
['p', 'y', 't', 'h', 'o', 'n']

字典推导式

>>> dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
>>> double_dict1 = {k:v*2 for (k,v) in dict1.items()}
>>> double_dict1
{'a': 2, 'b': 4, 'c': 6, 'd': 8, 'e': 10}

集合推导式

>>> set1 = {1,2,3,4}
>>> double_set = {i*2 for i in set1}
>>> double_set
{8, 2, 4, 6}

合并字典

>>> x = {'a':1,'b':2}
>>> y = {'c':3, 'd':4}
>>> z = {**x, **y}
>>> z
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

复制列表

>>> nums = [1,2,3]
>>> nums[::]
[1, 2, 3]
>>> copy_nums = nums[::]
>>> copy_nums
[1, 2, 3]

反转列表

>>> reverse_nums = nums[::-1]
>>> reverse_nums
[3, 2, 1]
 PACKING / UNPACKING

变量交换

>>> a,b = 1, 2
>>> a ,b = b,a
>>> a
2
>>> b
1

高级拆包

>>> a, *b = 1,2,3
>>> a
1
>>> b
[2, 3]

或者

>>> a, *b, c = 1,2,3,4,5
>>> a
1
>>> b
[2, 3, 4]
>>> c
5

函数返回多个值(其实是自动packing成元组)然后unpacking赋值给4个变量

>>> def f():
...   return 1, 2, 3, 4
...
>>> a, b, c, d = f()
>>> a
1
>>> d
4

列表合并成字符串

>>> " ".join(["I", "Love", "Python"])
'I Love Python'

链式比较

>>> if a > 2 and a < 5:
...   pass
...
>>> if 2<a<5:
...   pass
yield from
# 没有使用 field from
def dup(n):
  for i in range(n):
    yield i
    yield i
# 使用yield from
def dup(n):
  for i in range(n):
  yield from [i, i]
for i in dup(3):
  print(i)
>>>
0
0
1
1
2
2

in 代替 or

>>> if x == 1 or x == 2 or x == 3:
...   pass
...
>>> if x in (1,2,3):
...   pass

字典代替多个if else

def fun(x):
  if x == 'a':
    return 1
  elif x == 'b':
    return 2
  else:
    return None
def fun(x):
  return {"a": 1, "b": 2}.get(x)

有下标索引的枚举

>>> for i, e in enumerate(["a","b","c"]):
...   print(i, e)
...
0 a
1 b
2 c

生成器

注意区分列表推导式,生成器效率更高

>>> g = (i**2 for i in range(5))
>>> g
<generator object <genexpr> at 0x10881e518>
>>> for i in g:
...   print(i)
...
0
1
4
9
16

默认字典 defaultdict

>>> d = dict()
>>> d['nums']
KeyError: 'nums'
>>>
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> d["nums"]
[]

字符串格式化

>>> lang = 'python'
>>> f'{lang} is most popular language in the world'
'python is most popular language in the world'

列表中出现次数最多的元素

>>> nums = [1,2,3,3]
>>> max(set(nums), key=nums.count)
3

或者

from collections import Counter
>>> Counter(nums).most_common()[0][0]
3

读写文件

>>> with open("test.txt", "w") as f:
...   f.writelines("hello")

判断对象类型,可指定多个类型

>>> isinstance(a, (int, str))
True

类似的还有字符串的 startswith,endswith

>>> "http://foofish.net".startswith(('http','https'))
True
>>> "https://foofish.net".startswith(('http','https'))
True
__str__ 与 __repr__ 区别
>>> str(datetime.now())
'2018-11-20 00:31:54.839605'
>>> repr(datetime.now())
'datetime.datetime(2018, 11, 20, 0, 32, 0, 579521)'

前者对人友好,可读性更强,后者对计算机友好,支持 obj == eval(repr(obj))

使用装饰器

def makebold(f):
return lambda: "<b>" + f() + "</b>"
def makeitalic(f):
return lambda: "<i>" + f() + "</i>"
@makebold
@makeitalic
def say():
return "Hello"
>>> say()
<b><i>Hello</i></b>

不使用装饰器,可读性非常差

def say():
return "Hello"
>>> makebold(makeitalic(say))()
<b><i>Hello</i></b>

总结

以上所述是小编给大家介绍的Python 开发中的高级技巧,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

利用Hyperic调用Python实现进程守护

利用Hyperic调用Python,实现进程守护,供大家参考,具体内容如下 调用操作系统方法获取进程信息,判断进程是否存在,Linux和Windows均支持,区别在于获取进程信息和启动...

用Python中的wxPython实现最基本的浏览器功能

通常,大多数应用程序通过保持 HTML 简单来解决大多数浏览器问题 ― 或者说,根据最低共同特性来编写。然而,即便如此,也仍然存在字体和布局的问题,发行新浏览器和升级现有浏览器时,也免不...

Python实现基本数据结构中队列的操作方法示例

Python实现基本数据结构中队列的操作方法示例

本文实例讲述了Python实现基本数据结构中队列的操作方法。分享给大家供大家参考,具体如下: #! /usr/bin/env python #coding=utf-8 class Q...

python自动查询12306余票并发送邮箱提醒脚本

python自动查询12306余票并发送邮箱提醒脚本

由于车票难抢,有时需要的车票已经售空,而我们需要捡漏,便可使用这个脚本。 具体实现了,自动查询某一车票的余票数量,当数量产生变化时,将自动发送QQ邮件到对于的邮箱进行提醒。 其中,发送邮...

6行Python代码实现进度条效果(Progress、tqdm、alive-progress&#8203;&#8203;&#8203;&#8203;&#8203;&#8203;&#8203;和PySimpleGUI库)

6行Python代码实现进度条效果(Progress、tqdm、alive-progress&#8203;&#8203;&#8203;&#8203;&#8203;&#8203;&#8203;和PySimpleGUI库)

在项目开发过程中加载、启动、下载项目难免会用到进度条,如何使用Python实现进度条呢? 这里为小伙伴们分享四种Python实现进度条的库:Progress库、tqdm库、alive-p...