python3中常用的內建函式
從下圖中可以知道python3中有如下這些內建函式

圖一
下面我們一一瞭解下每個函式的用法:
abs()
abs()
函式是取絕對值的函式,在C語言只中有也有一個 abs()
函式但是在C語言中 abs()
函式的引數要求是整數,在C語言中如果要對一個浮點數取絕對值則應該用 fabs()
函式,但是在python中則不一樣, abs()
函式是對引數沒有要求的 ,不管是整數函式浮點數都可以,並且在使用 abs()
函式是你傳遞的引數是什麼型別的,返回的函式的型別也是相對應的型別
>>> abs(-1) 1 >>> abs(-2.0) 2.0 >>> type(abs(-2.4)) <class 'float'> >>> type(abs(-2)) <class 'int'>
all() any()
all()
和 any()
功能類似都是判斷一個可迭代物件是否為真
這兩個函式的返回值都是 boolean
值
首先我們應該瞭解在python3中哪些資料會被判斷為 False
a = 0# 整數0,False print(bool(a)) s = ""# 空字串,False print(bool(s)) lst = []# 空列表,False print(bool(lst)) dic = {}# 空字典,False print(bool(dic)) a = None# None表示空,真空,False print(bool(a))
除了以上資料,則都會被判斷為 True
all() any()的英文文件描述
all(iterable)
Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to: # 如果可迭代物件中的所有元素都是Ture則返回True,也可以理解為把可迭代物件中的所有元素做與運算 # 函式原型為 def all(iterable): for element in iterable: if not element: return False return True
例子
>>> all(['a',(2,4),1,True])#list都為真 True >>> all(['a',(),1,True])#list元素中有空tuple False >>> all(['a',(2,4),0,True])#有0 False >>> all(['a',(2,4),3,False])#有False False
any(iterable)
Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to: # 如果可迭代物件中存在元素是Ture則返回True,也可以理解為把可迭代物件中的所有元素做或運算 #函式原型為 def any(iterable): for element in iterable: if element: return True return False
例子
>>> any(['a',(2,4),3,True]) True >>> any(['a',(2,4),3,False]) True >>> any(['a',(),3,False]) True >>> any(['',(),0,False]) False >>> any(('a',(),3,False)) True >>> any(('',(),0,False)) False
ascii(object)
返回一個可列印的物件字串方式表示,如果是非ascii字元就會輸出\x,\u或\U等字元來表示。與python2版本里的repr()是等效的函式。
>>> ascii(1) '1' >>> ascii('$') "'$'" >>> ascii(999999) '999999' >>> ascii('中國') "'\\u4e2d\\u56fd'"
bin(),oct(), hex()
bin()
把十進位制轉化為二進位制
>>> bin(7) '0b111' >>> bin(-7) '-0b111' >>> bin(1.1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'float' object cannot be interpreted as an integer
oct()
把十進位制轉化為8進位制
>>> oct(10) '0o12' >>> oct(-10) '-0o12' >>> oct(-10.4) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'float' object cannot be interpreted as an integer
hex
把十進位制數轉化為16進位制
>>> hex(17) '0x11' >>> hex(-17) '-0x11' >>> hex(17.0) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'float' object cannot be interpreted as an integer
注意引數只能是整數
bool()
測試一個物件是True還是False.
>>> bool([]) False >>> bool(['']) True >>> bool(()) False >>> bool(0) False >>> bool(4) True >>> bool(1.1) True >>> bool({}) False >>> bool(['hello', 'world']) True
bytes()
將一個字串轉換成位元組型別
我們都知道在計算機的底層都是儲存的二進位制資訊不管是數字還是文字都是轉化為相對應的二進位制格式儲存在硬碟之中
我們都知道比如一個十進位制的數字 15
轉化為二進位制位 1111
,直接把 1111
儲存在計算機中,但是我們的文字文字資訊是怎麼轉化為這種二進位制資訊的呢?
這裡就要涉及到計算機中的編碼技術,我們最熟知的編碼無非就是 ASCII
編碼了,但是在 ASCII
編碼中並沒有中文字元的編碼規範,然後為了在計算機中表示中文,相應的相容中文的編碼規範就出臺了,比如有 GB2312
, GBK
, UTF-8
等等。 (最常用的還是 UTF-8
)
ofollow,noindex">UTF-8的具體規範
360?fr=aladdin" target="_blank" rel="nofollow,noindex">GBK規範
這裡我們只需要瞭解,一箇中文漢子用 GBK
編碼需要2位元組,用 UTF-8
需要三個位元組
例子
>>> bytes('中國', encoding='utf-8') b'\xe4\xb8\xad\xe5\x9b\xbd' >>> bytes('中國', encoding='UTF-8')# 編碼格式不區分大寫小寫 b'\xe4\xb8\xad\xe5\x9b\xbd' >>> bytes('中國', encoding='gbk') b'\xd6\xd0\xb9\xfa'
bytearray()
bytearray()
函式的官方文件描述是:
def __init__(self, source=None, encoding=None, errors='strict'): # known special case of bytearray.__init__ """ bytearray(iterable_of_ints) -> bytearray bytearray(string, encoding[, errors]) -> bytearray bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer bytearray(int) -> bytes array of size given by the parameter initialized with null bytes bytearray() -> empty bytes array Construct a mutable bytearray object from: - an iterable yielding integers in range(256) - a text string encoded using the specified encoding - a bytes or a buffer object - any object implementing the buffer API. - an integer # (copied from class doc) """ pass
- 當三個引數都不傳時
>>> b = bytearray() >>> b bytearray(b'') >>> len(b) 0
- 當source引數為字串時,encoding引數也必須提供,函式將字串使用str.encode方法轉換成位元組陣列
>>> bytearray('中文') Traceback (most recent call last): File "<pyshell#48>", line 1, in <module> bytearray('中文') TypeError: string argument without an encoding >>> bytearray('中文','utf-8') bytearray(b'\xe4\xb8\xad\xe6\x96\x87')
- 當source引數為整數時,返回這個整數所指定長度的空位元組陣列
>>> bytearray(2) bytearray(b'\x00\x00') >>> bytearray(-2) Traceback (most recent call last): File "<pyshell#51>", line 1, in <module> bytearray(-2) ValueError: negative count
傳遞的整數引數需大於0,使用來做陣列長度的
- 當source引數是一個可迭代物件,那麼這個迭代物件的元素都必須符合0 <= x < 256,以便可以初始化到數組裡
>>> bytearray([1,2,3]) bytearray(b'\x01\x02\x03') >>> bytearray([256,2,3]) #不在0-255範圍內報錯 Traceback (most recent call last): File "<pyshell#53>", line 1, in <module> bytearray([256,2,3]) ValueError: byte must be in range(0, 256)
callable()
callable()
函式用於檢查一個物件是否是可呼叫的。如果返回 True
, object
仍然可能呼叫失敗;但如果返回 False
,呼叫物件 ojbect
絕對不會成功。
對於 函式 , 方法 , lambda 函式 , 類 , 以及實現了 __call__
方法的類例項, 它都返回 True。
例子
>>>callable(0) False >>> callable("runoob") False >>> def add(a, b): ...return a + b ... >>> callable(add)# 函式返回 True True >>> class A:# 類 ...def method(self): ...return 0 ... >>> callable(A)# 類返回 True True >>> a = A() >>> callable(a)# 沒有實現 __call__, 返回 False False >>> class B: ...def __call__(self): ...return 0 ... >>> callable(B) True >>> b = B() >>> callable(b)# 實現 __call__, 返回 True True
chr() ord()
chr()
函式是把 Unicode
字元轉化為對應的十進位制數字
ord()
函式則相反這是把數字轉為對應的 Unicode
字元,不一定是十進位制其他進位制也可以
例子
>>> ord('你') 20320 >>>ord('a') 97 >>> ord('b') 98 >>> ord('c') 99 >>> ord('ab') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ord() expected a character, but string of length 2 found
>>> chr(97) 'a' >>> chr(65) 'A' >>> chr(20320) '你' >>> chr(3874298) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: chr() arg not in range(0x110000)
chr 的引數應該比0x110000小
compile() exec() eval()
compile(str, file, type)
我們知道我們寫出來的原始碼都是需要進行編譯或者翻譯成機器碼才能被我們的計算機執行, compile
語句的目的是提供一次性的位元組碼編譯,就不用在以後的每次呼叫中重新進行編譯了。
還需要注意的是,這裡的compile和正則表示式中使用的compile並不相同,儘管用途一樣。
例子
>>> c = compile(str,'<string>','exec') >>> c <code object <module> at 0x100dbfed0, file "<string>", line 1> >>> exec(c) 0 1 2
>>> str = 'print("hello")' >>> c = compile(str,'<string>','eval') >>> eval(c) hello
>>> single_code = compile( 'print("pythoner.com")', '<string>', 'single' ) >>> exec(single_code) pythoner.com >>> eval(single_code) pythoner.com
compile語句是從type型別(總共有三種類型包括' eval
',' exec
',' single
': 著山中型別分別的用法是, single
便是我們編譯的程式碼只有一行, eval
表示會把字串編譯成一個表示式, exec
表示會把字串編譯成一個和python一模一樣的東西,比如就可以有for 迴圈 )中將字串語句建立成程式碼物件。
eval()
eval()還可以單獨直接執行表示式
例子
>>> eval('3*2') 6 >>> eval('print("hello world!")') hello world!
exec()
exec()可以把符合python規範的字串當成python語句進行執行,如果直接用這種方式執行每次都得編譯一次
例子
>>> exec('for i in range(3): print(i)') 0 1 2 >>> exec('print(4)') 4 >>> exec('9*9')
eval執行表示式後是具有返回值的,但是exec是沒有返回值的
complex()
complex([real[, imag]])
complex()函式是處理複數的函式具體有如下用法:
例子
- 當兩個引數都不提供時,返回複數
0j
。(j
是python中的複數標誌,和我們數學課上學過的i
是一樣的)
>>> complex() 0j
- 當第一個引數為字串時,呼叫時不能提供第二個引數。此時字串引數,需是一個能表示複數的字串,而且加號或者減號左右不能出現空格。
>>> complex('2.3+9j') (2.3+9j) >>> complex('3+3j', 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: complex() can't take second arg if first is a string >>> complex('2 + 3j') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: complex() arg is a malformed string
- 當第一個引數為int或者float時,第二個引數可為空,表示虛部為0;如果提供第二個引數,第二個引數也需為int或者float。
>>> complex(2) (2+0j) >>> complex(3.1) (3.1+0j) >>> complex(3,4.2) (3+4.2j)
dict()
dict() 函式用於建立一個字典。返回一個字典
>>>dict()# 建立空字典 {} >>> dict(a='a', b='b', t='t')# 傳入關鍵字 {'a': 'a', 'b': 'b', 't': 't'} >>> dict(zip(['one', 'two', 'three'], [1, 2, 3]))# 對映函式方式來構造字典 {'three': 3, 'two': 2, 'one': 1} >>> dict([('one', 1), ('two', 2), ('three', 3)])# 可迭代物件方式來構造字典 {'three': 3, 'two': 2, 'one': 1}
dir()
函式不帶引數時,返回當前範圍內的變數、方法和定義的型別列表;帶引數時,返回引數的屬性、方法列表。如果引數包含方法 dir (),該方法將被呼叫。如果引數不包含 dir (),該方法將最大限度地收集引數資訊。
例子
dir() ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'i'] >>> dir(dict) ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] >>> dir([]) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
divmod()
函式把除數和餘數運算結果結合起來,返回一個包含商和餘數的元組(a // b, a % b)。
例子
>>>divmod(7, 2) (3, 1) >>> divmod(8, 2) (4, 0) >>> divmod(1+2j,1+0.5j)#複數也可以進行出發運算 ((1+0j), 1.5j) >>> div(12.3,7)# 只能進行整數的出發運算 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'div' is not defined
enumerate()
函式用於將一個可遍歷的資料物件(如列表、元組或字串)組合為一個索引序列,同時列出資料和資料下標,一般用在 for 迴圈當中。
例子
>>> enumerate([3,7,8]) <enumerate object at 0x10fc05948> >>> list(enumerate([3,7,8])) [(0, 3), (1, 7), (2, 8)] >>> list(enumerate([3,7,8], start=4)) [(4, 3), (5, 7), (6, 8)]
使用for循遍歷一個list
>>> i=0 >>> seq = ['pig', 'panda', 'duck'] >>> for ele in seq: ...print(i, ele) ...i+=1 ... 0 pig 1 panda 2 duck
結合enumerate使用for迴圈遍歷
>>> seq = ['pig', 'panda', 'duck'] >>> for n, ele in enumerate(seq): ...print(n, ele) ... 0 pig 1 panda 2 duck
id()
函式用於獲取物件的記憶體地址。
在python中既可以進行面對物件的程式設計,也可以面對過程的程式設計,但是總的來說 一切皆物件
,這句話不不容易理解的,需要我們對程式設計有深層次的學習後才能完全的體會到這句話的含義,但是我們不用著急學習的過程總是循序漸進的。
>>> id(seq) 4559234440 >>> id([]) 4559234568 >>> a = 1 >>> id(a) 4555139008
isinstance()
判斷一個一個物件是否是一個類的例項,或者判斷一個物件是否是一個類的子類的例項
這了的概念是和類有關的,如果看不懂則先去學習一個python面對物件程式設計
例子
>>> a = 1 >>> id(a) 4555139008 >>> isinstance(a, int) True >>> s = 'string object' >>> isinstance(s, str) True >>> isinstance(s, dict) False
filter()
filter() 函式用於過濾序列(對一個可迭代物件進行篩選,去掉不需要的元素),過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。
例子
需求去除序列[1,2,3,4,5,6]中的奇數
普通方法
>>> lis = [1,2,3,4,5,6] >>> ret = [] >>> def get_even_number_list(lis): ...for i in lis: ...if i % 2 == 0: ...ret.append(i) ... >>> get_even_number_list(lis) >>> ret [2, 4, 6]
使用 filter
方法
>>> isinstance(s, dict) False >>> lis = [1,2,3,4,5,6] >>> def is_even_number(x): ...if x%2 == 0: ...return True ...else: ...return False ... >>> list(filter(is_even_number, lis)) [2, 4, 6] >>> list(filter(lambda x:x%2==0, lis))#如果使用lambda表示式會更加簡單 [2, 4, 6]
從上面兩個的做法中我們就可以比較兩個方法,使用filter方法還是很方便的
map()
map()
函式的功能是接受兩個引數,前一個是一個方法,後一個是一個可迭代物件, map()
是把每一個可迭代物件中的元素去除做一定操作然後更新這個元素
例子
>>> lis = [101, 102, 103, 104, 105, 106] >>> map(lambda x:x-50, lis) <map object at 0x1064be588> >>> list(map(lambda x:x-50, lis)) [51, 52, 53, 54, 55, 56]
在python2.7以後 map
filter
函式的返回值是返回一個可迭代物件
globals() locals()
globals()
是獲取有的全域性變數
locals()
是獲取有的區域性變數
例子
>>> g = 1 >>> def f(): ...a = 2 ...print(globals()) ...print(locals()) ... >>> f() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'g': 1, 'f': <function f at 0x1018ebe18>}#存在的全域性變數 {'a': 2}#區域性變數
hash()
hash()
雜湊本來是密碼學中的一種訊息摘要演算法,用途很廣泛。如果像深入的瞭解什麼是雜湊函式,可以去百度上學習學習
例子
>>> s = 'hello' >>> hash(s) 779082262045361556 >>> type(hash(s)) <class 'int'>
int()
int()
函式用於將一個字串或數字轉換為整型。
int()
函式有兩個引數分別是 int(x, base=10)
- x -- 字串或數字。
- base -- 進位制數,預設十進位制。
例子
>>>int()# 不傳入引數時,得到結果0 0 >>> int(3) 3 >>> int(3.6) 3 >>> int('12',16)# 如果是帶引數base的話,12要以字串的形式進行輸入,12 為 16進位制 18 >>> int('0xa',16) 10 >>> int('10',8) 8
input()
大家都知道python有2.X和3.X版本的但是現在很多都是使用python3了,這隻只討論3版本。
在python3.X中raw_input( )和input( )進行了整合,在3.X版本中去除了raw_input( ),僅保留了input( )函式,其接收任意任性輸入,將所有輸入預設為字串處理,並返回字串型別。
如果像瞭解下python2.X的input函式可以去 菜鳥教程 上學習下這裡不做過多的講解了
在python3中的 input()
函式中所有的輸入都就被當成是字串
例子
>>> a = input('input:') input:string >>> type(a) <class 'str'> >>> i = int(input('input int number:')) # 這樣可以直接輸入一個整型 input int number:13 >>> type(i) <class 'int'> >>> i = float(input('輸入一個浮點數:'))#輸入一個浮點數 輸入一個浮點數:1.4 >>> type(i) <class 'float'>
len()
這是一個使用 非常頻繁 的函式,並且是一個很強大的函式
返回物件(字元、列表、元組等)長度或專案個數。
例子
>>> string = 'testing' >>> len(string) 7 >>> li = [1,2,3] >>> len(li) 3 >>> dic = {'1':3,'3':5} >>> len(dic) 2 >>> tu = (12,45,) >>> len(tu) 2
這裡提醒下python2中的 len()
函式在計算中文字串的長度的時候是以字串佔用的位元組數來計算的,下面請看例子:
Python 2.7.15 (default, May1 2018, 16:44:08) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> bytes('你是') '\xe4\xbd\xa0\xe6\x98\xaf' >>> len('你是') 6
但是在python3中, len()
函式還是以字元個數來計算長度
list()
把一個元組轉化為列表,或者一個可迭代的物件轉化為列表
例子
>>> str="Hello World" >>> list2=list(str) >>> list2 ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'] >>> aTuple = (123, 'Google', 'Runoob', 'Taobao') >>> list1 = list(aTuple) >>> print ("列表元素 : ", list1) 列表元素 :[123, 'Google', 'Runoob', 'Taobao']
max() min()
max()
min()
函式分別是區最大值和最小值
具體的可以看輸入的例子學習下面的用法
>>> max(80, 100, 1000) 1000 >>> max((1,2,3,4)) 4 >>> min((1,2,3,4)) 1 >>> s = '12345' >>> print(max(s)) 5 >>> print(max((),default=1)) 1 >>> print(max(())) #傳入可迭代物件為空時,必須指定引數default,用來返回預設值 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: max() arg is an empty sequence >>> s = [ ...{'name': 'sumcet', 'age': 18}, ...{'name': 'bbu', 'age': 11} ... ] >>> max(s, key=lambda x: x['age']) {'name': 'sumcet', 'age': 18} >>> prices = {'HPQ': 37.2, 'FB': 10.75, 'AAPL': 612.78, 'IBM': 205.55, 'ACME': 45.23} >>> max(prices, key=lambda k: prices[k]) 'AAPL'
min 和 max 的用法都是一樣的只是得到的結果是一個是最大一個是最小
pwd()
計算一個次方
例子
>>> 2**4 16 >>> pow(2, 4) 16
reversed()
reversed()
方法是取一有序序列的反序,可以是 tuple, string, list 或 range。
例子
>>> l = [1,2,3,4] >>> l.reverse()# str物件中的reverse方法頁具有反轉的功能 >>> l [4, 3, 2, 1] >>> reversed(l)# reversed() 的返回值是一個物件,如果可以我們可以把它轉為list或者是tuple <list_reverseiterator object at 0x10aa0a898> >>> l [4, 3, 2, 1] >>> l [4, 3, 2, 1] >>> list(reversed(l)) [1, 2, 3, 4] >>> tuple(reversed(l)) (1, 2, 3, 4) >>> list(reversed('AbcdefG')) ['G', 'f', 'e', 'd', 'c', 'b', 'A']
round()
round()
函式是取浮點數也就是小數的保留位數
round 有兩個引數,第一個是你要進行保留的小數,第二個是你像保留的位數,如果不傳值預設為0,也就是預設保留到個位。
例子
>>> round(70.23456) 70 >>> round(56.659,1) 56.7 >>> round(80.264, 2) 80.26 >>> round(-100.000056, 3) -100.0
set()
函式建立一個無序不重複元素集,可進行關係測試,刪除重複資料,還可以計算交集、差集、並集等。
例子
>>>x = set('runoob') >>> y = set('google') >>> x, y (set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l']))# 重複的被刪除 >>> x & y# 交集 set(['o']) >>> x | y# 並集 set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u']) >>> x - y# 差集 set(['r', 'b', 'u', 'n'])
range()
range(start, stop[, step])
特別常用
range函式返回的是一個可迭代物件(型別是物件),而不是列表型別, 所以列印的時候不會列印列表。
- 計數從 start 開始。預設是從 0 開始。例如range(5)等價於range(0, 5);
- 計數到 stop 結束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5。
- 步長,預設為1。例如:range(0, 5) 等價於 range(0, 5, 1)
例子
>>> range(10) range(0, 10) >>> range(2,10) range(2, 10) >>> range(1,4,2) range(1, 4, 2) >>> for i in range(1,4,2): ...print(i) ... 1 3 >>> for i in range(0,3,-1): ...print(i) ... >>> for i in range(3,0,-1): ...print(i) ... 3 2 1
zip()
函式用於將可迭代的物件作為引數,將物件中對應的元素打包成一個個元組,然後返回由這些元組組成的物件,這樣做的好處是節約了不少的記憶體。
我們可以使用 list() 轉換來輸出列表。
*
例子
壓縮
>>> a = [1,2,3] >>> b = [4,5,6] >>> v = [7,8,9] >>> zip(a,b,v) <zip object at 0x10aa0e848> >>> list(zip(a,b,v))# 把a,b,v壓縮在一起 [(1, 4, 7), (2, 5, 8), (3, 6, 9)] >>> v = [7,8,9,10] >>> list(zip(a,b,v))# 如果數量不一樣多則會丟掉多餘的,這裡就把10丟掉了 [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
解壓
>>> list(zip(a,b,v)) [(1, 4, 7), (2, 5, 8), (3, 6, 9)] >>> ret = zip(a,b,c) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'c' is not defined >>> ret = zip(a,b,v) >>> list(zip(*ret)) [(1, 2, 3), (4, 5, 6), (7, 8, 9)]