如何让你的Python代码更加pythonic ?

高洛峰
Libérer: 2016-10-17 17:22:33
original
1538 Les gens l'ont consulté

pythonic如果翻译成中文的话就是很python。很+名词结构的用法在中国不少,比如:很娘,很国足,很CCTV等等。·

以下为了简略,我们用P表示pythonic的写法,NP表示non-pythonic的写法,当然此P-NP非彼P-NP。

为什么要追求pythonic?

相比于NP,P的写法简练,明确,优雅,绝大部分时候执行效率高,代码越少也就越不容易出错。我认为好的程序员在写代码时,应该追求代码的正确性,简洁性和可读性,这恰恰就是pythonic的精神所在。

对于具有其他编程语言经验而初涉Python的程序员(比如我自己)来说,在写Python代码时,认识到pythonic的写法,会带来更多的便利和高效,而本文的主要读者也将是这群程序员。

以下将给出P和NP的n种示例,供读者参考,查阅。


P vs. NP的示例

链式比较

P:

a = 3
b = 1
1 <= b <= a < 10  #True
Copier après la connexion

NP:

a = 3
b = 1
b >= 1 and b <= a and a < 10 #True
Copier après la connexion

P是小学生都能看懂的语法,简单直接省代码~


真值测试

P:

name = &#39;Tim&#39;
langs = [&#39;AS3&#39;, &#39;Lua&#39;, &#39;C&#39;]
info = {&#39;name&#39;: &#39;Tim&#39;, &#39;sex&#39;: &#39;Male&#39;, &#39;age&#39;:23 }    
  
if name and langs and info:
    print(&#39;All True!&#39;)  #All True!
Copier après la connexion

NP:

if name != &#39;&#39; and len(langs) > 0 and info != {}:
    print(&#39;All True!&#39;) #All True!
Copier après la connexion

简而言之,P的写法就是对于任意对象,直接判断其真假,无需写判断条件,这样既能保证正确性,又能减少代码量。


真假值表(记住了假你就能省很多代码!)

真 假

True False

任意非空字符串 空的字符串 ''

任意非0数字 数字0

任意非空容器 空的容器 [] () {} set()

其他任意非False None


字符串反转

P:

def reverse_str( s ):
    return s[::-1]
Copier après la connexion

NP:

def reverse_str( s ):
    t = &#39;&#39;
    for x in xrange(len(s)-1,-1,-1):
        t += s[x]
    return t
Copier après la connexion

P的写法简单,经测试,效率也更好。

如果用于检测回文,就是一句话input == input[::-1],多么的优雅!


字符串列表的连接

P:

strList = ["Python", "is", "good"]  
  
res =  &#39; &#39;.join(strList) #Python is good
Copier après la connexion

NP:

res = &#39;&#39;
for s in strList:
    res += s + &#39; &#39;
#Python is good
#最后还有个多余空格
Copier après la connexion

string.join()常用于连接列表里的字符串,相对于NP,P的方式十分高效,且不会犯错。


列表求和,最大值,最小值,乘积

P:

numList = [1,2,3,4,5]   
sum = sum(numList)  #sum = 15
maxNum = max(numList) #maxNum = 5
minNum = min(numList) #minNum = 1
from operator import mul
prod = reduce(mul, numList, 1) #prod = 120 默认值传1以防空列表报错
Copier après la connexion


NP:

sum = 0
maxNum = -float(&#39;inf&#39;)
minNum = float(&#39;inf&#39;)
prod = 1
for num in numList:
    if num > maxNum:
        maxNum = num
    if num < minNum:
        minNum = num
    sum += num
    prod *= num
# sum = 15 maxNum = 5 minNum = 1 prod = 120
Copier après la connexion

经简单测试,在numList的长度为10000000时,在我的机器上对列表求和,P耗时0.6s,NP耗时1.3s,将近两倍的差距。所以不要自己造轮子了。


列表推导式

P:

l = [x*x for x in range(10) if x % 3 == 0]
#l = [0, 9, 36, 81]
Copier après la connexion

NP:

l = []
for x in range(10):
    if x % 3 == 0:
        l.append(x*x)
#l = [0, 9, 36, 81]
Copier après la connexion

你看,使用P的列表推导式,构建新列表变得多么简单直观!


字典的默认值

P:

dic = {&#39;name&#39;:&#39;Tim&#39;, &#39;age&#39;:23}  
  
dic[&#39;workage&#39;] = dic.get(&#39;workage&#39;,0) + 1
#dic = {&#39;age&#39;: 23, &#39;workage&#39;: 1, &#39;name&#39;: &#39;Tim&#39;}
Copier après la connexion

NP:

if &#39;workage&#39; in dic:
    dic[&#39;workage&#39;] += 1
else:
    dic[&#39;workage&#39;] = 1
#dic = {&#39;age&#39;: 23, &#39;workage&#39;: 1, &#39;name&#39;: &#39;Tim&#39;}
Copier après la connexion

dict的get(key,default)方法用于获取字典中key的值,若不存在该key,则将key赋默认值default。

P相比NP的写法少了if...else...,实乃痛恨if...else...之人首选!


for…else…语句

P:

for x in xrange(1,5):
    if x == 5:
        print &#39;find 5&#39;
        break
else:
    print &#39;can not find 5!&#39;
#can not find 5!
Copier après la connexion

NP:

find = False
for x in xrange(1,5):
    if x == 5:
        find = True
        print &#39;find 5&#39;
        break
if not find:
    print &#39;can not find 5!&#39;
#can not find 5!
Copier après la connexion

for...else...的else部分用来处理没有从for循环中断的情况。有了它,我们不用设置状态变量来检查是否for循环有break出来,简单方便。


三元符的替代

P:

a = 3  
  
b = 2 if a > 2 else 1
#b = 2
Copier après la connexion

NP:

if a > 2:
    b = 2
else:
    b = 1
#b = 2
Copier après la connexion

如果你具备C的编程经验,你就会寻找A ? B : C的替代品。你可能发现A and B or C看起来还不错,但是b = a > 1 and False or True会返回True,而实际意图应该返回False。

使用b = False if a > 1 else True则会正确返回False,所以它才是正宗的三元符替代品。


Enumerate

P:

array = [1, 2, 3, 4, 5]
  
for i, e in enumerate(array,0):
    print i, e
#0 1
#1 2
#2 3
#3 4
#4 5
Copier après la connexion

NP:

for i in xrange(len(array)):
    print i, array[i]
#0 1
#1 2
#2 3
#3 4
#4 5
Copier après la connexion

使用enumerate可以一次性将索引和值取出,避免使用索引来取值,而且enumerate的第二个参数可以调整索引下标的起始位置,默认为0。


使用zip创建键值对

P:

keys = [&#39;Name&#39;, &#39;Sex&#39;, &#39;Age&#39;]
values = [&#39;Tim&#39;, &#39;Male&#39;, 23]
  
dic = dict(zip(keys, values))
#{&#39;Age&#39;: 23, &#39;Name&#39;: &#39;Tim&#39;, &#39;Sex&#39;: &#39;Male&#39;}
Copier après la connexion

NP:

dic = {}
for i,e in enumerate(keys):
    dic[e] = values[i]
#{&#39;Age&#39;: 23, &#39;Name&#39;: &#39;Tim&#39;, &#39;Sex&#39;: &#39;Male&#39;}
Copier après la connexion

   

zip方法返回的是一个元组,用它来创建键值对,简单明了。


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!