小编给大家分享一下Python中常见的Pythonic写法有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
创新互联公司,为您提供成都网站建设、成都网站制作、网站营销推广、网站开发设计,对服务成都隧道混凝土搅拌车等多个行业拥有丰富的网站建设及推广经验。创新互联公司网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!“Programs must be written for people to read, and only incidentally for machines to execute.”
##不推荐
temp = a
a = b
b = a
##推荐
a, b = b, a # 先生成一个元组(tuple)对象,然后unpack
##不推荐
l = ['David', 'Pythonista', '+1-514-555-1234']
first_name = l[]
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
##不推荐
if fruit == "apple" or fruit == "orange" or fruit == "berry":
# 多次判断
##推荐
if fruit in ["apple", "orange", "berry"]:
# 使用 in 更加简洁
##不推荐
colors = ['red', 'blue', 'green', 'yellow']
result = ''
for s in colors:
result += s # 每次赋值都丢弃以前的字符串对象, 生成一个新对象
##推荐
colors = ['red', 'blue', 'green', 'yellow']
result = ''.join(colors) # 没有额外的内存分配
##不推荐
for key in my_dict.keys():
# my_dict[key] ...
##推荐
for key in my_dict:
# my_dict[key] ...
# 只有当循环中需要更改key值的情况下,我们需要使用 my_dict.keys()
# 生成静态的键值列表。
##不推荐
if my_dict.has_key(key):
# ...do something with d[key]
##推荐
if key in my_dict:
# ...do something with d[key]
##不推荐
navs = {}
for (portfolio, equity, position) in data:
if portfolio not in navs:
navs[portfolio] =
navs[portfolio] += position * prices[equity]
##推荐
navs = {}
for (portfolio, equity, position) in data:
# 使用 get 方法
navs[portfolio] = navs.get(portfolio, ) + position * prices[equity]
# 或者使用 setdefault 方法
navs.setdefault(portfolio, )
navs[portfolio] += position * prices[equity]
##不推荐
if x == True:
# ....
if len(items) != :
# ...
if items != []:
# ...
##推荐
if x:
# ....
if items:
# ...
##不推荐
items = 'zero one two three'.split()
# method 1
i =
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
##不推荐
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)]
##不推荐
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...
##不推荐
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
##不推荐
def my_range(n):
i =
result = []
while i < n:
result.append(fn(i))
i += 1
return result # 返回列表
##推荐
def my_range(n):
i =
result = []
while i < n:
yield fn(i) # 使用生成器代替列表
i += 1
*尽量用生成器代替列表,除非必须用到列表特有的函数。
##不推荐
reduce(rf, filter(ff, map(mf, a_list)))
##推荐
from itertools import ifilter, imap
reduce(rf, ifilter(ff, imap(mf, a_list)))
*lazy evaluation 会带来更高的内存使用效率,特别是当处理大数据操作的时候。
##不推荐
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...
##不推荐
class Clock(object):
def __init__(self):
self.__hour = 1
def setHour(self, hour):
if 25 > hour > : 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 > : self.__hour = hour
else: raise BadHourException
def __getHour(self):
return self.__hour
hour = property(__getHour, __setHour)
##不推荐
f = open("some_file.txt")
try:
data = f.read()
# 其他文件操作..
finally:
f.close()
##推荐
with open("some_file.txt") as f:
data = f.read()
# 其他文件操作...
##不推荐
try:
os.remove("somefile.txt")
except OSError:
pass
##推荐
from contextlib import ignored # Python 3 only
with ignored(OSError):
os.remove("somefile.txt")
##不推荐
import threading
lock = threading.Lock()
lock.acquire()
try:
# 互斥操作...
finally:
lock.release()
##推荐
import threading
lock = threading.Lock()
with lock:
# 互斥操作...
以上是“Python中常见的Pythonic写法有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联-成都网站建设公司行业资讯频道!