当前位置:主页 > python教程 > Python高级编程技巧

Python高级编程技巧总结

发布:2018-10-22 16:23:53 59


为找教程的网友们整理了相关的编程文章,网友方思思根据主题投稿了本篇教程内容,涉及到Python、编程技巧、Python高级编程技巧相关内容,已被304网友关注,相关难点技巧可以阅读下方的电子资料。

Python高级编程技巧

符合语言习惯的 Python 优雅编程技巧

Python最大的优点之一就是语法简洁,好的代码就像伪代码一样,干净、整洁、一目了然。要写出 Pythonic(优雅的、地道的、整洁的)代码,需要多看多学大牛们写的代码,github 上有很多非常优秀的源代码值得阅读,比如:requests、flask、tornado,下面列举一些常见的Pythonic写法。
0. 程序必须先让人读懂,然后才能让计算机执行。

“Programs must be written for people to read, and only incidentally for machines to execute.”

1. 交换赋值

##不推荐
temp = a
a = b
b = a 
##推荐
a, b = b, a # 先生成一个元组(tuple)对象,然后unpack

2. Unpacking

##不推荐
l = ['David', 'Pythonista', '+1-514-555-1234']
first_name = l[0]
last_name = l[1]
phone_number = l[2] 
##推荐
l = ['David', 'Pythonista', '+1-514-555-1234']
first_name, last_name, phone_number = l
# Python 3 Only
first, *middle, last = another_list

3. 使用操作符in

##不推荐
if fruit == "apple" or fruit == "orange" or fruit == "berry":
  # 多次判断 
##推荐
if fruit in ["apple", "orange", "berry"]:
  # 使用 in 更加简洁

4. 字符串操作

##不推荐
colors = ['red', 'blue', 'green', 'yellow']
result = ''
for s in colors:
  result += s # 每次赋值都丢弃以前的字符串对象, 生成一个新对象 
##推荐
colors = ['red', 'blue', 'green', 'yellow']
result = ''.join(colors) # 没有额外的内存分配

5. 字典键值列表

##不推荐
for key in my_dict.keys():
  # my_dict[key] ... 
##推荐
for key in my_dict:
  # my_dict[key] ...
# 只有当循环中需要更改key值的情况下,我们需要使用 my_dict.keys()
# 生成静态的键值列表。

6. 字典键值判断

##不推荐
if my_dict.has_key(key):
  # ...do something with d[key] 
##推荐
if key in my_dict:
  # ...do something with d[key]

7. 字典 get 和 setdefault 方法

##不推荐
navs = {}
for (portfolio, equity, position) in data:
  if portfolio not in navs:
      navs[portfolio] = 0
  navs[portfolio] += position * prices[equity]
##推荐
navs = {}
for (portfolio, equity, position) in data:
  # 使用 get 方法
  navs[portfolio] = navs.get(portfolio, 0) + position * prices[equity]
  # 或者使用 setdefault 方法
  navs.setdefault(portfolio, 0)
  navs[portfolio] += position * prices[equity]

8. 判断真伪

##不推荐
if x == True:
  # ....
if len(items) != 0:
  # ...
if items != []:
  # ... 
##推荐
if x:
  # ....
if items:
  # ...

9. 遍历列表以及索引

##不推荐
items = 'zero one two three'.split()
# method 1
i = 0
for item in items:
  print i, item
  i += 1
# method 2
for i in range(len(items)):
  print i, items[i]
##推荐
items = 'zero one two three'.split()
for i, item in enumerate(items):
  print i, item

10. 列表推导

##不推荐
new_list = []
for item in a_list:
  if condition(item):
    new_list.append(fn(item)) 
##推荐
new_list = [fn(item) for item in a_list if condition(item)]

11. 列表推导-嵌套

##不推荐
for sub_list in nested_list:
  if list_condition(sub_list):
    for item in sub_list:
      if item_condition(item):
        # do something... 
##推荐
gen = (item for sl in nested_list if list_condition(sl) \
      for item in sl if item_condition(item))
for item in gen:
  # do something...

12. 循环嵌套

##不推荐
for x in x_list:
  for y in y_list:
    for z in z_list:
      # do something for x & y 
##推荐
from itertools import product
for x, y, z in product(x_list, y_list, z_list):
  # do something for x, y, z

13. 尽量使用生成器代替列表

##不推荐
def my_range(n):
  i = 0
  result = []
  while i < n:
    result.append(fn(i))
    i += 1
  return result # 返回列表
##推荐
def my_range(n):
  i = 0
  result = []
  while i < n:
    yield fn(i) # 使用生成器代替列表
    i += 1
*尽量用生成器代替列表,除非必须用到列表特有的函数。

14. 中间结果尽量使用imap/ifilter代替map/filter

##不推荐
reduce(rf, filter(ff, map(mf, a_list)))
##推荐
from itertools import ifilter, imap
reduce(rf, ifilter(ff, imap(mf, a_list)))
*lazy evaluation 会带来更高的内存使用效率,特别是当处理大数据操作的时候。

15. 使用any/all函数

##不推荐
found = False
for item in a_list:
  if condition(item):
    found = True
    break
if found:
  # do something if found... 
##推荐
if any(condition(item) for item in a_list):
  # do something if found...

16. 属性(property)

=
##不推荐
class Clock(object):
  def __init__(self):
    self.__hour = 1
  def setHour(self, hour):
    if 25 > hour > 0: self.__hour = hour
    else: raise BadHourException
  def getHour(self):
    return self.__hour
##推荐
class Clock(object):
  def __init__(self):
    self.__hour = 1
  def __setHour(self, hour):
    if 25 > hour > 0: self.__hour = hour
    else: raise BadHourException
  def __getHour(self):
    return self.__hour
  hour = property(__getHour, __setHour)

17. 使用 with 处理文件打开

##不推荐
f = open("some_file.txt")
try:
  data = f.read()
  # 其他文件操作..
finally:
  f.close()
##推荐
with open("some_file.txt") as f:
  data = f.read()
  # 其他文件操作...

18. 使用 with 忽视异常(仅限Python 3)

##不推荐
try:
  os.remove("somefile.txt")
except OSError:
  pass
##推荐
from contextlib import ignored # Python 3 only
with ignored(OSError):
  os.remove("somefile.txt")

19. 使用 with 处理加锁

##不推荐
import threading
lock = threading.Lock()
lock.acquire()
try:
  # 互斥操作...
finally:
  lock.release()
##推荐
import threading
lock = threading.Lock()
with lock:
  # 互斥操作...

20. 参考

1) Idiomatic Python: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
2) PEP 8: Style Guide for Python Code: http://www.python.org/dev/peps/pep-0008/

有关Python的22个编程技巧

1. 原地交换两个数字

Python 提供了一个直观的在一行代码中赋值与交换(变量值)的方法,请参见下面的示例:

x,y= 10,20
print(x,y)
x,y= y,x
print(x,y)
#1 (10, 20)
#2 (20, 10)

赋值的右侧形成了一个新的元组,左侧立即解析(unpack)那个(未被引用的)元组到变量 <a> 和 <b>。

一旦赋值完成,新的元组变成了未被引用状态并且被标记为可被垃圾回收,最终也完成了变量的交换。

2. 链状比较操作符

比较操作符的聚合是另一个有时很方便的技巧:

n= 10
result= 1< n< 20
print(result)
# True
result= 1> n<= 9
print(result)
# False

3. 使用三元操作符来进行条件赋值

三元操作符是 if-else 语句也就是条件操作符的一个快捷方式:

[表达式为真的返回值] if [表达式] else [表达式为假的返回值]

这里给出几个你可以用来使代码紧凑简洁的例子。下面的语句是说“如果 y 是 9,给 x 赋值 10,不然赋值为 20”。如果需要的话我们也可以延长这条操作链。

x = 10 if (y == 9) else 20

同样地,我们可以对类做这种操作:

x = (classA if y == 1 else classB)(param1, param2)

在上面的例子里 classA 与 classB 是两个类,其中一个类的构造函数会被调用。

下面是另一个多个条件表达式链接起来用以计算最小值的例子:

def small(a,b,c):
returnaifa<= banda<= celse(bifb<= aandb<= celsec)
print(small(1,0,1))
print(small(1,2,2))
print(small(2,2,3))
print(small(5,4,3))
#Output
#0 #1 #2 #3

我们甚至可以在列表推导中使用三元运算符:

[m**2 if m > 10 else m**4 for m in range(50)]
#=> [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]

4. 多行字符串

基本的方式是使用源于 C 语言的反斜杠:

multiStr= “select * from multi_row
where row_id < 5”
print(multiStr)
# select * from multi_row where row_id < 5

另一个技巧是使用三引号:

multiStr= “””select * from multi_row
where row_id < 5″””
print(multiStr)
#select * from multi_row
#where row_id < 5

上面方法共有的问题是缺少合适的缩进,如果我们尝试缩进会在字符串中插入空格。所以最后的解决方案是将字符串分为多行并且将整个字符串包含在括号中:

multiStr= (“select * from multi_row ”
“where row_id < 5 ”
“order by age”)
print(multiStr)
#select * from multi_row where row_id < 5 order by age

5. 存储列表元素到新的变量中

我们可以使用列表来初始化多个变量,在解析列表时,变量的数目不应该超过列表中的元素个数:【译者注:元素个数与列表长度应该严格相同,不然会报错】

testList= [1,2,3]
x,y,z= testList
print(x,y,z)
#-> 1 2 3

6. 打印引入模块的文件路径

如果你想知道引用到代码中模块的绝对路径,可以使用下面的技巧:

import threading
import socket
print(threading)
print(socket)
#1- <module ‘threading' from ‘/usr/lib/python2.7/threading.py'>
#2- <module ‘socket' from ‘/usr/lib/python2.7/socket.py'>

7. 交互环境下的 “_” 操作符

这是一个我们大多数人不知道的有用特性,在 Python 控制台,不论何时我们测试一个表达式或者调用一个方法,结果都会分配给一个临时变量: _(一个下划线)。

>>> 2+ 1
3
>>> _
3
>>> print_
3
“_” 是上一个执行的表达式的输出。

8. 字典/集合推导

与我们使用的列表推导相似,我们也可以使用字典/集合推导,它们使用起来简单且有效,下面是一个例子:

testDict= {i: i *iforiinxrange(10)}
testSet= {i *2foriinxrange(10)}
print(testSet)
print(testDict)
#set([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
#{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

注:两个语句中只有一个 <:> 的不同,另,在 Python3 中运行上述代码时,将 <xrange> 改为 <range>。

9. 调试脚本

我们可以在 <pdb> 模块的帮助下在 Python 脚本中设置断点,下面是一个例子:

import pdb
pdb.set_trace()

我们可以在脚本中任何位置指定 <pdb.set_trace()> 并且在那里设置一个断点,相当简便。

10. 开启文件分享

Python 允许运行一个 HTTP 服务器来从根路径共享文件,下面是开启服务器的命令:

# Python 2
python -m SimpleHTTPServer
# Python 3
python3 -m http.server

上面的命令会在默认端口也就是 8000 开启一个服务器,你可以将一个自定义的端口号以最后一个参数的方式传递到上面的命令中。

11. 检查 Python 中的对象

我们可以通过调用 dir() 方法来检查 Python 中的对象,下面是一个简单的例子:

test= [1,3,5,7]
print(dir(test))
[‘__add__', ‘__class__', ‘__contains__', ‘__delattr__', ‘__delitem__', ‘__delslice__', ‘__doc__', ‘__eq__', ‘__format__', ‘__ge__', ‘__getattribute__', ‘__getitem__', ‘__getslice__', ‘__gt__', ‘__hash__', ‘__iadd__', ‘__imul__', ‘__init__', ‘__iter__', ‘__le__', ‘__len__', ‘__lt__', ‘__mul__', ‘__ne__', ‘__new__', ‘__reduce__', ‘__reduce_ex__', ‘__repr__', ‘__reversed__', ‘__rmul__', ‘__setattr__', ‘__setitem__', ‘__setslice__', ‘__sizeof__', ‘__str__', ‘__subclasshook__', ‘append', ‘count', ‘extend', ‘index', ‘insert', ‘pop', ‘remove', ‘reverse', ‘sort']

12. 简化 if 语句

我们可以使用下面的方式来验证多个值:

if m in [1,3,5,7]:

而不是:

if m==1 or m==3 or m==5 or m==7:

或者,对于 in 操作符我们也可以使用 ‘{1,3,5,7}' 而不是 ‘[1,3,5,7]',因为 set 中取元素是 O(1) 操作。

13. 一行代码计算任何数的阶乘

Python 2.x.
result= (lambdak: reduce(int.__mul__,range(1,k+1),1))(3)
print(result)
#-> 6
Python 3.x.
import functools
result= (lambdak: functools.reduce(int.__mul__,range(1,k+1),1))(3)
print(result)
#-> 6

14. 找到列表中出现最频繁的数

test= [1,2,3,4,2,2,3,1,4,4,4]
print(max(set(test),key=test.count))
#-> 4

15. 重置递归限制

Python 限制递归次数到 1000,我们可以重置这个值:

import sys
x=1001
print(sys.getrecursionlimit())
sys.setrecursionlimit(x)
print(sys.getrecursionlimit())
#1-> 1000
#2-> 1001

请只在必要的时候采用上面的技巧。

16. 检查一个对象的内存使用

在 Python 2.7 中,一个 32 比特的整数占用 24 字节,在 Python 3.5 中利用 28 字节。为确定内存使用,我们可以调用 getsizeof 方法:

在 Python 2.7 中

import sys
x=1
print(sys.getsizeof(x))
#-> 24
在 Python 3.5 中
import sys
x=1
print(sys.getsizeof(x))
#-> 28

17. 使用 __slots__ 来减少内存开支

你是否注意到你的 Python 应用占用许多资源特别是内存?有一个技巧是使用 __slots__ 类变量来在一定程度上减少内存开支。

import sys
classFileSystem(object):
def __init__(self,files,folders,devices):
self.files= files
self.folders= folders
self.devices= devices
print(sys.getsizeof(FileSystem))
classFileSystem1(object):
__slots__= [‘files','folders','devices']
def __init__(self,files,folders,devices):
self.files= files
self.folders= folders
self.devices= devices
print(sys.getsizeof(FileSystem1))
#In Python 3.5
#1-> 1016
#2-> 888

很明显,你可以从结果中看到确实有内存使用上的节省,但是你只应该在一个类的内存开销不必要得大时才使用 __slots__。只在对应用进行性能分析后才使用它,不然地话,你只是使得代码难以改变而没有真正的益处。

【译者注:在我的 win10 python2.7 中上面的结果是:

#In Python 2.7 win10
#1-> 896
#2-> 1016

所以,这种比较方式是不那么让人信服的,使用 __slots__ 主要是用以限定对象的属性信息,另外,当生成对象很多时花销可能会小一些,具体可以参见 python 官方文档:

The slots declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because dict is not created for each instance. 】

18. 使用 lambda 来模仿输出方法

import sys
lprint=lambda *args:sys.stdout.write(” “.join(map(str,args)))
lprint(“python”,”tips”,1000,1001)
#-> python tips 1000 1001

19.从两个相关的序列构建一个字典

t1= (1,2,3)
t2= (10,20,30)
print(dict(zip(t1,t2)))
#-> {1: 10, 2: 20, 3: 30}

20. 一行代码搜索字符串的多个前后缀

print(“http://www.google.com”.startswith((“http://”,”https://”)))
print(“http://www.google.co.uk”.endswith((“.com”,”.co.uk”)))
#1-> True
#2-> True

21. 不使用循环构造一个列表

import itertools
test= [[-1,-2],[30,40],[25,35]]
print(list(itertools.chain.from_iterable(test)))
#-> [-1, -2, 30, 40, 25, 35]

22. 在 Python 中实现一个真正的 switch-case 语句

下面的代码使用一个字典来模拟构造一个 switch-case。

def xswitch(x):
returnxswitch._system_dict.get(x,None)
xswitch._system_dict= {‘files': 10,'folders': 5,'devices': 2}
print(xswitch(‘default'))
print(xswitch(‘devices'))
#1-> None
#2-> 2

 


参考资料

相关文章

  • Python基于回溯法子集树模板解决旅行商问题详解

    发布:2019-12-22

    这篇文章主要介绍了Python基于回溯法子集树模板解决旅行商问题(TSP),简单描述了旅行商问题并结合实例形式分析了Python使用回溯法子集树模板解决旅行商问题的相关实现步骤与操作技巧,需要


  • python中的正则表达式,贪婪匹配与非贪婪匹配方式

    发布:2023-04-24

    这篇文章主要介绍了python中的正则表达式,贪婪匹配与非贪婪匹配方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教


  • Python中time库的使用(日期时间)

    发布:2023-04-20

    time库是python中处理时间的标准库,这篇文章主要介绍了Python中time库的使用(日期时间),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下


  • python3的url编码和解码应用实例

    发布:2020-01-30

    今天小编就为大家分享一篇python3的url编码和解码,自定义gbk、utf-8的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


  • python去除列表中的空值元素实战技巧

    发布:2023-04-21

    这篇文章主要介绍了python实战技巧之去除列表中的空值元素,搜集针对python高效处理数据的核心代码,今天是实现去除列表中的空值元素,需要的朋友可以参考下


  • python telnetlib模块安装实例

    发布:2019-06-24

    在本篇内容里小编给大家整理了关于python telnetlib模块安装的实例代码内容,需要的朋友们参考学习下。


  • python中pip的使用和修改下载源的实例操作方法

    发布:2019-11-20

    这篇文章主要介绍了python中pip的使用和修改下载源的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧


  • Python写一个量化股票提醒系统实例

    发布:2019-06-22

    这篇文章主要介绍了小白用Python写了一个股票提醒系统,迷你版量化系统,完美的实现了实时提醒功能,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下


网友讨论