描述

Python 元组 tuple() 方法用于将可迭代对象(字符串、列表、元祖、字典)转换为元组。

语法

tuple() 方法语法:

tuple(iterable)

参数

  • iterable -- 要转换为元组的可迭代对象(字符串、列表、元祖、字典)。

返回值

返回元组。

实例

以下实例展示了 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/python3

aList = [123, 'xyz', 'zara', 'abc']
aTuple = tuple(aList)
print ("Tuple elements : ", aTuple)

以上实例输出结果为:

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

相关文章:

  • 2021-06-06
  • 2021-11-18
  • 2021-06-01
  • 2021-12-20
  • 2021-04-26
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-18
  • 2021-12-13
  • 2021-11-09
  • 2021-06-29
  • 2022-12-23
  • 2021-10-19
相关资源
相似解决方案