python常用函数的用法_python中常用函数整理
1、map
map是python內(nèi)置的高階函數(shù),它接收一個(gè)函數(shù)和一個(gè)列表,函數(shù)依次作用在列表的每個(gè)元素上,返回一個(gè)可迭代map對象。
class map(object):""" map(func, *iterables) --> map objectMake an iterator that computes the functionusing arguments fromeach of the iterables. Stops when the shortest iterableisexhausted.""" def __getattribute__(self, *args, **kwargs): # real signature unknown"""Return getattr(self, name)."""pass
def __init__(self, func,*iterables): # real signature unknown; restored from__doc__
pass
def __iter__(self,*args, **kwargs): # real signature unknown"""Implement iter(self)."""pass
@staticmethod # knowncaseof __new__
def __new__(*args, **kwargs): # real signature unknown"""Create and return a new object. See help(type) for accurate signature."""pass
def __next__(self,*args, **kwargs): # real signature unknown"""Implement next(self)."""pass
def __reduce__(self,*args, **kwargs): # real signature unknown"""Return state information for pickling."""pass
用法舉例 : 將列表li中的數(shù)值都加1, li = [1,2,3,4,5]
li = [1,2,3,4,5]
def add1(x):return x+1res=map(add1, li)
print(res)for i inres:
print(i)
結(jié)果:
2
3
4
5
6
2、lambda表達(dá)式
是一個(gè)表達(dá)式,可以創(chuàng)建匿名函數(shù),冒號前是參數(shù),冒號后只能有一個(gè)表達(dá)式(傳入?yún)?shù),根據(jù)參數(shù)表達(dá)出一個(gè)值)
nl = lambda x,y:x*y # 給出x,y參數(shù),計(jì)算出x和y的相乘
print(nl(3,5))
pring(-----)
#和map的結(jié)合
li= [1,2,3,4,5]for i in map(lambda x:x*2, li):
print(i)
結(jié)果:15
-----
2
4
6
8
10
3、Pool
1、多進(jìn)程,是multiprocessing的核心,它與threading很相似,但對多核CPU的利用率會比threading好的多
2、可以允許放在Python程序內(nèi)部編寫的函數(shù)中,該P(yáng)rocess對象與Thread對象的用法相同,擁有is_alive()、join([timeout])、run()、start()、terminate()等方法
3、multiprocessing包中也有Lock/Event/Semaphore/Condition類,用來同步進(jìn)程
傳統(tǒng)的執(zhí)行多個(gè)函數(shù)的例子
import time
def do_proc(n): # 返回平方值
time.sleep(1)return n*nif __name__ == '__main__':
start=time.time()for p in range(8):
print(do_proc(p)) # 循環(huán)執(zhí)行8個(gè)函數(shù)
print("execute time is" ,time.time()-start)
結(jié)果:0
1
4
9
16
25
36
49execute timeis 8.002938985824585
使用多進(jìn)程執(zhí)行函數(shù)
import timefrommultiprocessing import Pool
def do_proc(n): # 返回平方值
time.sleep(n)
print(n)return n*nif __name__ == '__main__':
pool= Pool(3) # 池中最多只能放三個(gè)任務(wù)
start=time.time()
p1= pool.map(do_proc, range(8)) # 跟python的map用法相似(map連續(xù)生成8個(gè)任務(wù)的同時(shí)依次傳給pool,pool依次調(diào)起池中的任務(wù),執(zhí)行完的任務(wù)從池中剔除)
pool.close() # 關(guān)閉進(jìn)程池
pool.join() # 等待所有進(jìn)程(8個(gè)進(jìn)程)的結(jié)束
print(p1)
print("execute time is", time.time() -start)
結(jié)果:0
1
2
3
4
5
6
7[0, 1, 4, 9, 16, 25, 36, 49]
execute timeis 3.3244528770446777
查看任務(wù)管理器:
4、random
import random
print(random.random()) # 生成一個(gè)0-1隨機(jī)小數(shù)
print(random.uniform(10,20)) # 指定范圍隨機(jī)選擇一個(gè)小數(shù)
print(random.randint(10,20)) # 指定范圍內(nèi)隨機(jī)選擇一個(gè)整數(shù)
print(random.randrange(0,90,2)) # 指定范圍內(nèi)選擇一個(gè)隨機(jī)偶數(shù)
print(random.choice('abcdefg')) # 指定字符串中隨機(jī)選擇一個(gè)字符
print(random.sample('abcdefgh'),2) # 指定字符串內(nèi)隨機(jī)選擇2個(gè)字符
print(random.choice(['app','pear','ora'])) # 指定列表內(nèi)隨機(jī)選擇一個(gè)值
itmes= [1,2,3,4,5,6,7,8] # 將列表表洗牌
random.shuffle(itmes)
print(itmes)
總結(jié)
以上是生活随笔為你收集整理的python常用函数的用法_python中常用函数整理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java todo error_java
- 下一篇: python中pop用法_Python