Python Tuple(元組) tuple()方法

Python 元組Python 元組


描述

Python 元組 tuple() 函數將列表轉換為元組。

語法

tuple()方法語法:

tuple( seq )

參數

  • seq -- 要轉換為元組的序列。

返回值

返回元組。

實例

以下實例展示了 tuple()函數的使用方法:

實例 1

>>>tuple([1,2,3,4]) (1, 2, 3, 4) >>> tuple({1:2,3:4}) #針對字典 會返回字典的key組成的tuple (1, 3) >>> tuple((1,2,3,4)) #元組會返回元組自身 (1, 2, 3, 4)

實例 2

#!/usr/bin/python aList = [123, 'xyz', 'zara', 'abc']; aTuple = tuple(aList) print "Tuple elements : ", aTuple

以上實例輸出結果為:

Tuple elements :  (123, 'xyz', 'zara', 'abc')

Python 元組Python 元組