Python Tuple(元组) tuple()方法

时间:2020-07-26 15:47:52   收藏:0   阅读:73

描述

Python 元组 tuple() 函数将列表转换为元组。每组词 www.cgewang.com

语法

tuple()方法语法:

tuple( 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/python aList = [123, xyz, zara, abc]; aTuple = tuple(aList) print "Tuple elements : ", aTuple

以上实例输出结果为:

Tuple elements :  (123, ‘xyz‘, ‘zara‘, ‘abc‘)
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!