Python高級技巧(一)

1 列表解析 和 filter lambda

filter(lambda x: x>0 , list)

[ x for x in data if x >= 0]

列表

from random import randint

data = [ randint(-10,10) for _ in range(10)]
res = filter(lambda x : x>=0 , data)
aa = [ x for x in data if x >= 0]

字典

from random import randint

d = {x:randint(50,100) for x in range(1,21)}
res = {s:c for s,c in d.items() if c>=90}
print(res)

集合

data = [-5, -7, -6, 7, 4, -3, -8, -9, 7, 0]
se = set(data)
res = { x for x in se if x%3==0}
aa = filter(lambda x:x%3==0 , se)

另外:測試時間的方法

# 測一下時間消耗
# (進入 ipython)
data = [-5, -7, -6, 7, 4, -3, -8, -9, 7, 0]
timeit filter(lambda x : x>=0 , data)
timeit [ x for x in data if x >= 0]

2 命名、統計 (collections)

命名

# 元組查找極快,元素不可修改。
NAME,AGE,SEX,EMAIL = range(4)

student = ('jim',16,'male','[email protected]')

print(student[EMAIL])

內置元組

from collections import namedtuple

student = namedtuple('Student',['name','age','sex','email'])
s=student('jim',16,'male','[email protected]')
print(s.name)

統計元素

data = [6, 1, 8, 1, 8, 2, 8, 4, 8, 6, 3, 4, 6, 3, 4, 5, 2, 9, 8, 3]
c = dict.fromkeys(data,0)
for x in data:
    c[x]+=1
print(c)
# 加強版
from collections import Counter

data = [6, 1, 8, 1, 8, 2, 8, 4, 8, 6, 3, 4, 6, 3, 4, 5, 2, 9, 8, 3]
c2 = Counter(data)
print(c2)
print(c2[1],c2[8])
# most_common 找出出現頻率最高的三個元素
print(c2.most_common(3))  

統計英文文章單詞

import re
from collections import Counter

with open('secret.txt') as f:
    text = f.read()
textlist = re.split('\W+',text)
print(len(textlist))
c3 = Counter(re.split('\W+',text))
# print(c3)
print(c3.most_common(6))

3 提取公共鍵 (map reduce)

兩種方法

from random import randint,sample
from functools import reduce

left = 2
right = 6
s1 = {x:randint(0,3) for x in sample('abcdefg',randint(left,right))}
s2 = {x:randint(0,3) for x in sample('abcdefg',randint(left,right))}
s3 = {x:randint(0,3) for x in sample('abcdefg',randint(left,right))}

# 1.4 µs ± 55.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
print(s1.keys() & s2.keys() & s3.keys())

# 2.14 µs ± 36.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
reduce(lambda a,b:a&b , map(dict.keys,[s1,s2,s3]))

幫助理解

from random import randint,sample

aa = sample('abcdefg',randint(1,5))
print(aa)
from random import randint,sample

left = 2
right = 6
s1 = {x:randint(0,3) for x in sample('abcdefg',randint(left,right))}
s2 = {x:randint(0,3) for x in sample('abcdefg',randint(left,right))}
s3 = {x:randint(0,3) for x in sample('abcdefg',randint(left,right))}
m = map(dict.keys,[s1,s2,s3])
print(list(m))
from functools import reduce
def f(x, y):
    return x + y

# reduce第一個參數是 函數 , 第二個參數是 列表、集合、元組或者map 之類可迭代的對象
reduce(f,[1,3,5,7,9],100)    #第三個參數不要也行,意義是 設置初始值

4 隊列、pickle

代碼如下

deque隊列 以及 pickle持續化存儲對象(不用放在內存裏面)

from random import randint
from collections import deque
import pickle

history = deque(maxlen=5)
N=randint(0,100)

def guess(k):
    if k == N:
        print('=====(`_`)=====')
        return True
    elif k > N:
        print("too big")
    else:
        print('too small')

while True:
    line = input("guess it ! :")
    if line.isdigit():
        k = int(line)
        history.append(k)
        guess(k)
        with open('history.txt','wb') as f:
            pickle.dump(history,f)
    elif line == "h":
        with open('history.txt','rb') as f:
            his = pickle.load(f)
        print(his)

幫助理解

import pickle
from random import randint

alist = [randint(0,100) for _ in range(10)]

with open('testnote','wb') as f:
    pickle.dump(alist,f)

with open('testnote','rb') as f:
    alist = pickle.load(f)

print(alist)

pickle和json的區別 (python3)

  1. json 和 pickle 都是存儲到硬盤上 持久保存
  2. json被各種語言支持 , pickle只用來Python程序之間使用。
  3. json在Python裏面只支持列表字典 , pickle支持Python的所有數據類型。
import pickle
from random import randint
import json

aa = [randint(0,100) for _ in range(10)]
atuple = tuple(aa)
aset = set(aa)
adic = {'a':1 , 'b':2, 'c':3}

def ppickle(obj):
    filename = 'picklenote'
    with open(filename,'wb') as f:
        pickle.dump(obj,f)

    with open(filename,'rb') as f:
        res = pickle.load(f)
    print(res)


def jjson(obj):
    filename = 'jsonnote'
    with open(filename,'w') as f:
        json.dump(obj,f)

    with open(filename,'r') as f:
        res = json.load(f)
    print(res)

print("\n+pickle:")
ppickle(aa)
ppickle(atuple)
ppickle(adic)
ppickle(aset)

print("\n+json:")
jjson(aa)
jjson(atuple)
jjson(adic)
# jjson(aset)  # TypeError: Object of type 'set' is not JSON serializable
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章