标签:语法 highlight example 字典 elements blog 使用 方法 list
Python 元组 tuple() 方法将字符串、列表、字典等转换为元组。
tuple()方法语法:
tuple( seq )
返回元组。
以下实例展示了 tuple()函数的使用方法:
>>>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)
#!/usr/bin/python3
aList = [123, ‘xyz‘, ‘zara‘, ‘abc‘]
aTuple = tuple(aList)
print ("Tuple elements : ", aTuple)
以上实例输出结果为:
Tuple elements : (123, ‘xyz‘, ‘zara‘, ‘abc‘)
标签:语法 highlight example 字典 elements blog 使用 方法 list
原文地址:http://www.cnblogs.com/wushuaishuai/p/7729945.html