Python3 tuple 函数

返回到:Python3 内置函数

描述

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

语法

以下是 tuple 的语法:

tuple( iterable )

参数

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

返回值

返回元组。

实例

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

>>>list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
>>> tuple1=tuple(list1)
>>> tuple1
('Google', 'Taobao', 'Web176', 'Baidu')

补充

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

>>> 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')

返回到:Python3 内置函数

作者:terry,如若转载,请注明出处:https://www.web176.com/python3_api/9438.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年2月10日
下一篇 2023年2月10日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注