一. 列表、字典、集合、元組的使用
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)鎮(zhèn)寧免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了數(shù)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。from random import randint, sample
# 列表解析
data = [randint(-10, 10) for _ in xrange(10)]
filter(lambda x: x >= 0, data)
[x for x in data if x >= 0] #最快速
# 字典解析
d = {x: randint(60, 100) for x in xrange(1,21)}
{k : v for k, v in d.iteritems() if v > 90}
# 集合解析
s = set(data)
{x for x in s if x % 3 ==0}
# 元組
student = ('Jim', 16, 'male', 'jim@qq.com')
# 1. enum
NAME, AGE, SEX, EMAIL = xrange(4)
print student[NAME]
# 2.
from collections import namedtuple
Student = namedtuuple('Student', ['name', 'age', 'sex', 'email'])
s2 = Student('Tom', 16, 'mail', 'tom@qq.com')
print s2.name
# 統(tǒng)計(jì)列表的重復(fù)元素
li = [randint(0, 20) for _ in xrange(30)]
d = dict.fromkeys(li, 0)
# 1.
for x in li: d[x] += 1
# 2.
from collections import Counter
d2 = Counter(li)
d2.most_common(3) #重復(fù)數(shù)高的三個(gè)元素
# 字典根據(jù)值value排序
sorted([3, 1, 5]) #排序列表
(97, 'a') > (88, 'b') # 元組的比較,每個(gè)元素從開始比較
d = {x: randint(60, 100) for x in 'abcde'}
# 1.
data = zip(d.itervalues(), d.iterkeys())
sorted(data)
# 2.
sorted(d.items(), key=lambda x: x[1])
# 多個(gè)字典中的公共鍵
sample('abcdefg', 3)
sample('abcdefg', randint(3,6)) # 隨機(jī)取出幾個(gè)元素
s1 = {x: randint(1,4) for x in sample('abcdefg', randint(3.6))}
s2 = {x: randint(1,4) for x in sample('abcdefg', randint(3.6))}
s3 = {x: randint(1,4) for x in sample('abcdefg', randint(3.6))}
# 1.
res = []
for k in s1: if k in s2 and k in s3: res.append(k) # res.pop(k)
# 2. 使用集合
res = s1.viewkeys() & s2.viewkeys() & s3.viewkeys()
# 3.
m1 = map(dict.viewkeys, [s1, s2, s3])
res = reduce(lambda a, b: a & b, m1)
# 保持字典有序
d = {'Jim':(1, 35), 'Leo':(2, 38), 'Tom':(3, 44)}
from collections import OrderedDict
d = OrderedDict() #按進(jìn)入字典的順序打印
d['Jim'] = (1,35)
d['Leo'] = (2,38)
d['Tom'] = (3,44)
from time import time
start = time()
raw_input() #等待輸入
# ...
timesecond = time() - start
# 歷史記錄
# 1. 用隊(duì)列存儲(chǔ)
from collections import deque
q = deque([], 5)
q.append(1) # 達(dá)到長(zhǎng)度后,先進(jìn)先出
li = list(q) # 轉(zhuǎn)換成列表類型
# 2. 將q存到文件中
import pickle
pickle.dump(q, open('test','w'))
q2 = pickle.load(open('test','r'))
二. 迭代器、生成器
# 實(shí)現(xiàn)可迭代對(duì)象、迭代器對(duì)象
# 用時(shí)訪問 并封裝到一個(gè)對(duì)象中
# 可迭代對(duì)象
li = [1,2,3,4]
str= 'abcde'
# 迭代器
iterl = iter(li) # li.__iter__()
iters = iter(str) # str.__getitem__()
iterl.next()
# 1. 城市天氣的迭代器和可迭代對(duì)象
from collections import Iterable, Iterator
class WIterator(Iterator):
def __init__(self, cities):
self.cities = cities
self.index = 0
def getWeather(city):
import requests
r = requests.get(u'http://wthrcdn.etouch.cn/weather_mini?city=' + city)
data = r.json()['data']['forecast'][0]
return '%s: %s, %s' % (city, data['low'], data['high'])
def next(self):
if self.index == len(self.cities)
raise StopIteration
city = self.cities[self.index]
self.index += 1
return self.getWeather(city)
class WIterable(Iterable):
def __init__(self, cities):
self.cities = cities
def __iter__(self):
return WIterator(self.cities)
for x in WIterable([u'北京', u'長(zhǎng)沙', u'廣州']):
print x
# 2. 使用生成器函數(shù)實(shí)現(xiàn)可迭代對(duì)象
def f():
print 'in f(), 1'
yield 1
print 'in f(), 2'
yield 2
g = f() # g.__iter__()
for i in g: print i
class PrintNumbers:
def __init__(self, start, end):
self.start = start
self.end = end
def isPrimeNum(self, k):
if k % 2 == 0:
return True
else:
return False
def __iter__(self):
for k in xrange(self.start, self.end+1):
if self.isPrimeNum(k):
yield k
for x in PrintNumbers(1, 100): print x
# 進(jìn)行反向迭代
li = [1,2,3,4,5]
li.reverse() # 改變?cè)瓉砹斜?/p>
li[::-1] # 切片,和原來列表等大的新列表
ll = li.reversed(li) # 列表反向迭代
for i in ll: print i
class FloatRange:
def __init__(self, start, end, step=0.1)
self.start = start
self.end = end
self.step = step
def __iter__(self):
t = self.start
while t <= self.end:
yield t
t += self.step
def __reversed__(self):
t = self.enf
while t >= self.start:
yield t
t -= self.step
# 正向迭代
for i in FloatRange(1.0, 4.0, 0.5): print x
# 反向迭代
for i in reversed(FloatRange(1.0, 4.0, 0.5)): print x
# 對(duì)迭代器做切片操作
from itertools import islice
# islice()
li = range(20)
t = iter(li)
for x in islice(t, 5, 10): print x # 會(huì)消耗原來的迭代對(duì)象
# 在一個(gè)for中迭代多個(gè)可迭代對(duì)象
chinese = [randint(60,100) for _ in xrange(40)]
math = [randint(60,100) for _ in xrange(40)]
english = [randint(60,100) for _ in xrange(40)]
for in in xrange(len(math)):
print chinese[i] + math[i] + english[i]
total = []
# 并行多個(gè)可迭代對(duì)象
for c, m, e in zip(chiness, math, english)
print c+m+e
# 1. 串連多個(gè)迭代對(duì)象
from itertools import chain
c1 = [randint(60,100) for _ in xrange(40)]
c2 = [randint(60,100) for _ in xrange(42)]
c3 = [randint(60,100) for _ in xrange(45)]
for s in chain(c1, c2, c3):
if s > 90: print s
三. 字符串
# 拆分含多種分隔符的字符串
s = "fwerf sd123 ^sdf dfdsf*d dsf 123"
s.split(“xy”) #默認(rèn)以空格分割,或以參數(shù)分割
res = s.split(";")
map(lambda x: x.split("|"), res) # 以";"和"|"分割的二維數(shù)組
t = []
map(lambda x: t.extend(x.split("|")), res) # 二維元素放到t中
# 1.
def aSplit(s, ds):
res = [s]
for d in ds:
t = []
map(lambda x: t.extend(x.split(d)), res)
res = t
return res
print aSplit(s, " ^*") # 會(huì)存在空的元素
# 2. 正則表達(dá)式
import re
re.split(r'[,;|]+', s)
# 判斷字符串a(chǎn)是否以b開頭或結(jié)尾
#s.startswith() s.endswith() 接收單個(gè)字符串或字符串元組
import os, stat
files = [name for name in os.listdir(".") if name.endswith(('.sh', '.py'))]
# 調(diào)整字符串中文本的格式
#日志中'yyyy-mm-dd' 改為 'mm/dd/yyyy'
import re
log = open("/var/log/dpkg.log").read()
re.sub('(\d{4})-(\d{2})-(\d{2})', r'\2/\3/\1', log)
re.sub('(?P\d{4})-(?P\d{2})-(?P\d{2})', r'\g/\g/\g', log)
# 多個(gè)小字符串拼接成大字符串
s1 = "abcde"
s2 = "12345"
s1 + s2 # str.__add__(s1, s2) str.__gt__(s1, s2) 運(yùn)算符重載
s = ""
for p in pl: s += p # 變量多時(shí),臨時(shí)變量開銷大,資源浪費(fèi)
s.join(s1) # 參數(shù)可為字符串,可為列表
li = ['avc', 123, 'xyz', 456]
''.join([str(x) for x in li]) #列表解析,會(huì)生成一個(gè)列表,開銷大
''.join(str(x) for x in li) #生成器, (str(x) for x in li) 作為參數(shù)是括號(hào)省略
# 字符串格式對(duì)齊
# str.ljust() str.rjust() str.center()
s = "abc"
s.ljust(10 ,'=') # 左對(duì)齊,填充=
s.center(10)
format(s, '<20') # 左對(duì)齊
format(s, '>20') # 右對(duì)齊
format(s, '^20') # 居中
# 去掉字符串中不需要的字符
s = ' -------sd dfadf 2332 +++++++++'
s.strip(' -+')
s.lstrip()
s.rstirp()
# 刪除固定位置的字符,拼接切片
s[:3]+ s[4:]
# 替換
s.replace('\t', '')
import re
re.sub('[\t\r]', '', s)
s = 'abc123e3rxyz'
#s.translate()
import string
tr = string.maketrans('abcxyz', 'xyzabc')
s.translate(tr)
s = 'abc\rdfd\n234234\t'
s.translate(None, '\r\t\n')
四. 文件讀寫
# python2 str unicode
# python3 bytes str
# python2
s = u'你好'
s.encode('utf8') #存儲(chǔ)到文件中的格式
f = open('hello.txt', 'w')
f.write(s.encode('utf8'))
f.close()
f = open('hello.txt', 'r')
t = f.read().decode('utf8') # 你好
f.close()
# python3 字符串就是unicode
strb = b'asdfasdfsdg'
s = '你好'
f = open('hello2.txt', 'wt', encoding='utf8') # 自動(dòng)完成編解碼
f.write(s)
f.close()
f = open('hello2.txt', 'rt', encoding='utf8')
s = f.read()
f.close()
# 處理二進(jìn)制文件 處理音頻文件,將音量調(diào)小保存
f = open('demo.wav', 'rb')
info = f.read(44) #文件頭
import struct
struct.unpack('h',info[22:24]) #處理文件頭 數(shù)據(jù)運(yùn)算
struct.unpack('i',infi[24:28])
f.seek(0,2)
f.tell()
n = (f.tell()-44) /2
import array
buf = array.array('h', (0 for _ in xrange(n)))
f.seek(44)
f.readinto(buf)
for i in xrange(n): buf[i] /= 8
f2 = open('demo2.wav', 'wb')
f2.write(info)
buf.tofile(f2)
f2.close()
# 使用臨時(shí)文件
# 自動(dòng)刪除,不占內(nèi)存
from tempfile import TemporaryFile, NamedTemporaryFile
f = TemporaryFile() # 系統(tǒng)文件系統(tǒng)找不到
f.write('abcddee'*100000)
f.seek(0)
f.read(100)
ntf = NamedTemporaryFile(delete=False) # 能找到文件,默認(rèn)關(guān)閉以后會(huì)刪除文件
fname = nft.name
# 設(shè)置文件的緩沖
# I/O 操作以塊為單位,如4096字節(jié)一個(gè)塊
f = open('test.txt', 'w', buffering=2048) # 全緩沖,要寫滿緩沖才會(huì)寫到文件中
f = open('test.txt', 'w', buffering=1) # 行緩沖,\n就會(huì)寫文件
f = open('test.txt', 'w', buffering=1) # 無緩沖,實(shí)時(shí)寫
f.write('abc')
# 將文件映射到內(nèi)存
import mmap
f = open('demo.bn','r+b')
f.fileno()
m = mmap.mmap(f.fileno(), 0, access=mmpa.ACCESS_WRITE, offset=mmap.PAGESIZE)
# 得到字節(jié)數(shù)組
m[4:8] = '\xff'*4 # 修改直接改變文件內(nèi)容
# 讀寫csv數(shù)據(jù)
from urllib import urlretrieve
urlretrieve('http://table.finance.yahoo.com/table.csv?s=000001.sz', 'pingan.csv')
rf = open('pingan.csv', 'rb')
import csv
reader = csv.reader(rf)
header = reader.next()
wf = open('pingan_c.csv', 'wb')
writer = csv.writeer(wf)
writer.writerow(header)
rf.close()
wf.close()
# 讀寫json數(shù)據(jù)
import requests
import json
from record import Record
record = Record(channel=1)
audioData = record.record(2)
from secret import API_KEY, SECRET_KEY
authUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + API_KEY + "&client_secret=" + SECRET_KEY
response = requests.get(authUrl)
res = json.loads(response.content)
token = res['access_token']
#百度語音識(shí)別
cuid = 'xxxxxxxxxxxxx'
srvUrl = 'http://vop.baidu.com/server_api?cuid=' + cuid + '&token=' + token
heepHeader = {'Content-Type': 'audio/wav; rate = 8000'}
response = requests.post(srvUrl, headers=httpHeader, data=audioData)
res = json.loads(response.content)
text = res['result'][0]
print text
# json.dumps() python對(duì)象(列表、字典等)轉(zhuǎn)換成json字符串
# json.dumps(data, sort_keys=True)
# json.loads() json字符串轉(zhuǎn)換成python對(duì)象
with open('demo.json', 'wb') as f:
json.dump(l, f) # 將l數(shù)據(jù)寫到文件
# 構(gòu)建xml文檔
from xml.etree.ElementTree import parse
with open('demo.xml') with f:
et = parse(f)
root = et.getroot()
root.tag
root.attrib
root.text
#root.getchildren()
for child in root:
print child.get('name')
root.find('country')
root.findall('country') # 直接子元素
for e in root.iterfind('country'):
print e.get('name')
from xml.etree.ElementTree import Element, ElementTree, tostring
e = Element('Data')
e.set('name', 'abc')
e2 = Element('Row')
e3 = Element('Open')
e3.text = '8.80'
e2.append(e3)
e.append(e2)
tostring(e)
et = ElementTree(e)
et.write('demo.xml')
# 讀寫excel文件
import xlrd, xlwt
book = xlrd.open_workbook('demo.xls')
book.sheets()
sheet = book.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
cell = sheet.cell(0,0) #(0,0)單元格
cell.ctype
cell.value
row = sheet.row(1) #cell對(duì)象列表
data = sheet.row_values(1, 1) #第1列跳過第一格的值列表
sheet.put_cell(0, cols, xlrd.XL_CELL_TEXT, u'Total', None)
wbook = xlwt.Workbook()
wsheet = wbook.add_sheet('sheet1')
style = xlwt.easyxf('align: vertical center, horizontal center')
wsheet.write(rows,cols, sheet.cell_value(rows,cols), style)
wsheet.save('output.xls')
五. 派生內(nèi)置不可變類型并修改其實(shí)例化行為
class IntTuple(tuple):
def __new__(cls, iterable): #先于__init__()調(diào)用
g = (x for x in iterable if isinstance(x, int) and x > 0)
super(IntTuple, cls).__new__(cls, g)
def __init__(self, iterable):
# 此時(shí)如果過濾iterable 無法過濾成功
super(IntTuple, self).__init__(iterable)
t = IntTuple([1, -1, 'abc', 6, ['x', 'y'], 3])
print t?無錫婦科醫(yī)院排名 http://www.csyhjlyy.com/
六. 使用描述符對(duì)實(shí)例屬性做類型檢查
# 描述符: 包含 __get__() __set__() __delete__() 函數(shù)的類
class Attr(object):
def __init__(self, name, type_):
self.name = name
self.type_= type_
def __get__(self, instance, cls):
return instanse.__dict__[self.name]
def __set__(self, instance, value):
if not isinstance(value, self.type_):
raise TypeError('expected %s' % self.type_)
instance.__dict__[self.name] = value
def __delete__(self, instance):
del instance.__dict__[self.name]
class Person(object):
name = Attr('name', str)
age = Attr('age', int)
hgt = Attr('height', float)
p = Person()
p.name = 'Bob'
print p.name
p.age = '17' #會(huì)拋出異常
七. 在環(huán)狀數(shù)據(jù)結(jié)構(gòu)中管理內(nèi)存
import sys
class A(object):
def __del__(self): # 當(dāng)引用次數(shù)變?yōu)?時(shí),調(diào)用析構(gòu)函數(shù)
print 'in A.__del__'
a = A()
a2 = a
print sys.getrefcount(a) - 1 # 查看對(duì)象a的引用次數(shù),參數(shù)名也引用了對(duì)象,要-1
del a
del a2
# 循環(huán)引用
class Data(object): # Data類保存Node對(duì)象引用
def __init__(self, value, owner):
self.owner = owner
self.value = value
def __str__(self):
return "%s's data, value is %s" % (self.owner, self.value)
def __del__(self):
print 'in Data.__del__'
class Node(object): # Node類調(diào)用Data對(duì)象
def __init__(self, valu):
self.data = Data(value, self)
def __del__(self):
print 'in Node.__del__'
node = Node(100)
del node # 此時(shí)Data Node不會(huì)被回收掉
raw_input('wait...')
# 使用弱引用
import weakref
a_wref = weakref.ref(a)
a2 = a_wref()
class Data(object): # Data類保存Node對(duì)象引用
def __init__(self, value, owner):
self.owner = weakref.ref(owner) # 弱引用
self.value = value
def __str__(self):
return "%s's data, value is %s" % (self.owner(), self.value)
def __del__(self):
print 'in Data.__del__'
node2 = node(100)
del node2 # 此時(shí)Data Node將被回收
八. 通過實(shí)例方法名的字符串調(diào)用方法
# Circle Triangle Trctangle 求面積的方法名都不同
# 通過傳方法名來調(diào)用不同的方法
# 1. getattr 獲取對(duì)象屬性,方法名也是屬性
from lib1 import Circle
from lib2 import Triangle
from lib3 import Tectangle
def getArea(shape):
for name in ('area', 'getArea', get_area):
f = getattr(shape, name , None)
if f:
return f()
shape1 = Circle(2)
shape2 = Tirangle(3,4,5)
shape3 = Rectangle(6,4)
shapes = [shape1, shape2, shape3]
print map(getArea, shapes)
# 2. 使用opreator標(biāo)準(zhǔn)庫(kù)
from opreator import methodcaller
s = "abc123abc456"
s.find('abc', 4)
methodcaller('find', 'abc', 4)(s)
九. 為創(chuàng)建大量實(shí)例節(jié)省內(nèi)存
class Player(object):
def __init__(self, uid, name, status=0, level=1):
self.uid = uid
self.name = name
self.stat = status
self.level = level
class Player2(object):
__slots__ = ['uid', 'name', 'stat', 'level']
def __init__(self, uid, name, status=0, level=1):
self.uid = uid
self.name = name
self.stat = status
self.level = level
p1 = Player('0001', 'Jim')
p2 = Player2('0002', 'Tom')
# p1 bi p2 多兩個(gè)屬性 __dict__ __weakref__
# __dict__ 字典,為實(shí)例動(dòng)態(tài)綁定解除新屬性
# p2 則不能動(dòng)態(tài)綁定屬性
# __slots__ 阻止了該功能
十. 讓對(duì)象支持上下文管理
# 要使用上下文管理,類中要定義 __enter__ __exit__方法,分別在with開始和結(jié)束時(shí)調(diào)用
class test(object):
...
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
pass
with test() as k:
pass
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站標(biāo)題:分享Python的十個(gè)小技巧-創(chuàng)新互聯(lián)
本文鏈接:http://jinyejixie.com/article46/dijhhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)、定制網(wǎng)站、面包屑導(dǎo)航、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容