標籤:運行 self == color level base64編碼 async tool 關於
這裡記錄一些python的一些基礎知識,主要內容是高階函數的使用。或許我的心包有一層硬殼,能破殼而入的東西是極其有限的。所以我才不能對人一往情深。
python中的高階函數一、map()、reduce()和filter()函數使用
map()函數接收兩個參數,一個是函數,一個是Iterable,map將傳入的函數依次作用到序列的每個元素,並把結果作為新的Iterator返回。
def f(x): return x * xprint(list(map(f, range(1, 7)))) # [1, 4, 9, 16, 25, 36]print(list(map(lambda x: x * x, range(1, 7)))) # [1, 4, 9, 16, 25, 36]
reduce把一個函數作用在一個序列[x1, x2, x3, ...]上,這個函數必須接收兩個參數,reduce把結果繼續和序列的下一個元素做累積計算。
from functools import reducedef add(x, y): return x + yprint(reduce(add, range(1, 10))) # 45print(reduce(lambda x, y: x + y, range(1, 10))) # 45
filter()函數用於過濾序列。
def is_odd(n): return n % 2 == 0print(list(filter(is_odd, range(1, 10)))) # [2, 4, 6, 8]print(list(filter(lambda x: x % 2 == 0, range(1, 10)))) # [2, 4, 6, 8]
sorted()函數用於排序。
def ownSort(n): return str(abs(n))[0]sortList = [-3, 9, -7, 10]print(sorted(sortList)) # [-7, -3, 9, 10]print(sorted(sortList, key=abs)) # [-3, -7, 9, 10]print(sorted(sortList, key=abs, reverse=True)) # [10, 9, -7, -3]print(sorted(sortList, key=ownSort)) # [10, -3, -7, 9]
二、關於python變數的範圍理解
def scope_test(): def do_local(): spam = "local spam" def do_nonlocal(): nonlocal spam spam = "nonlocal spam" def do_global(): global spam spam = "global spam" spam = "test spam" do_local() print("After local assignment:", spam) do_nonlocal() print("After nonlocal assignment:", spam) do_global() print("After global assignment:", spam) scope_test()print("In global scope:", spam)# After local assignment: test spam# After nonlocal assignment: nonlocal spam# After global assignment: nonlocal spam# In global scope: global spam
Note how the local assignment (which is default) didn’t change scope_test’s binding of spam. The nonlocal assignment changed scope_test’s binding of spam, and the global assignment changed the module-level binding.
三、python中協程的一個案例
import asyncioimport randomimport threadingasync def Hello(index): print(‘Hello world! index=%s, thread=%s‘ % (index, threading.currentThread())) await asyncio.sleep(random.randint(1, 5)) print(‘Hello again! index=%s, thread=%s‘ % (index, threading.currentThread()))loop = asyncio.get_event_loop()tasks = [Hello(1), Hello(2)]loop.run_until_complete(asyncio.wait(tasks))loop.close()
啟動並執行結果如下:
Hello world! index=2, thread=<_MainThread(MainThread, started 37900)>Hello world! index=1, thread=<_MainThread(MainThread, started 37900)>Hello again! index=1, thread=<_MainThread(MainThread, started 37900)>Hello again! index=2, thread=<_MainThread(MainThread, started 37900)>
四、python中的base64編碼與解碼
import base64# base64編碼m = base64.b64encode(b‘my name is huhx.‘)print(m) # b‘bXkgbmFtZSBpcyBodWh4Lg==‘# # base64解碼bytes = base64.b64decode(m)print(bytes) # b‘my name is huhx.‘
codecs模組的簡單使用
print(‘中國‘.encode(‘utf-8‘)) # b‘\xe4\xb8\xad\xe5\x9b\xbd‘print(‘中國‘.encode(‘gbk‘)) # b‘\xd6\xd0\xb9\xfa‘import codecsprint(codecs.encode(‘中國‘, ‘utf-8‘)) # b‘\xe4\xb8\xad\xe5\x9b\xbd‘print(codecs.encode(‘中國‘, ‘gbk‘)) # b‘\xd6\xd0\xb9\xfa‘
str對象的encode方法的文檔如下:
def encode(self, encoding=‘utf-8‘, errors=‘strict‘): # real signature unknown; restored from __doc__ """ S.encode(encoding=‘utf-8‘, errors=‘strict‘) -> bytes Encode S using the codec registered for encoding. Default encoding is ‘utf-8‘. errors may be given to set a different error handling scheme. Default is ‘strict‘ meaning that encoding errors raise a UnicodeEncodeError. Other possible values are ‘ignore‘, ‘replace‘ and ‘xmlcharrefreplace‘ as well as any other name registered with codecs.register_error that can handle UnicodeEncodeErrors. """ return b""
友情連結
python基礎---->python的使用(五)