描述

tuple 函数将可迭代系列(如列表)转换为元组。

语法

以下是 tuple 的语法:

1
tuple( iterable )

参数

  • iterable – 要转换为元组的可迭代序列。

返回值

返回元组。


实例

以下展示了使用 tuple 的实例:

实例

1
2
3
4
>>>list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
>>> tuple1=tuple(list1)
>>> tuple1
('Google', 'Taobao', 'Runoob', 'Baidu')

tuple() 可以将字符串,列表,字典,集合转化为元组;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> a= 'www'
>>> b=tuple(a)
>>> b
('w', 'w', 'w')
>>> a={'www':123,'aaa':234}
>>> b=tuple(a)
>>> b
('www', 'aaa') # 将字段转换为元组时,只保留键!
>>> a=set('abcd')
>>> print (a)
{'c', 'd', 'b', 'a'}
>>> b=tuple(a)
>>> b
('c', 'd', 'b', 'a')