# -*- coding: utf-8 -*- """ Created on Tue Jun 03 21:17:59 2014 @author: Administrator """ # conversions and immutability T = ( 'cc', 'aa', 'dd', 'bb' ) tmp = list( T ) tmp.sort() print tmp T = tuple( tmp ) print T ################################## T = ( 1, 2, 3, 4, 5 ) L = [ x + 20 for x in T ] print L ################################## T = ( 1, [ 2, 3 ], 4 ) # error! T[1] = 'spam' T[1][0] = 'spam'
tuple可看作对象的一种简单的关联,而list可视作一种数据结构,随着时间而改变
tuple与其他编程语言中的常量声明有点相似,可用作list不可担当的地方—作为字典的键值