Python中list的append方法添加,和使用下标取得。之中的元素还可以是其他的数据结构

来源:互联网 发布:网络推广软件哪个好 编辑:程序博客网 时间:2024/05/18 22:42
其中元组和列表最大的一个区别,目前我觉得就是元组是不可修改的,没有append等方法。
#pylist_test.pyclass Test:i = 0def __init__(self):passdef sayhello(self):print 'hello'+str(self.i)Test.i = self.i+1def geti(self):print self.idef __del__(self):print 'destruct'+str(self.i)def main():li = list()j = int()j = 0seq = (1,2,3,4,5)#print len(seq)for i in seq:for i in seq:li.append(Test())li[j].sayhello()j = j + 1print "come here 01!"li[i-1].geti()print len(li)print "come here o2!"main()#print len(li)print "come here 03!"#Tuple To List#>>> li = list((1,2))#[1,2]#List To Tuple#>>> tu = tuple([1,2])#(1,2)#we can declare a specific type variable obviously#size = int()#size = 1#print type(size),size#size = 'test'#print type(size),size#list_test = list()#print type(list_test)#t1 = Test()#t2 = Test()#t3 = Test()#list_test.append(t1)#list_test.append(t2)#list_test.append(t3)#list_test[0].sayhello()#list_test[1].sayhello()#list_test[2].sayhello()#list can be nested in Python#list_2 = list()#list_2.append(list_test)#list_2[0][0].sayhello()#list_2[0][1].sayhello()#list_2[0][2].sayhello()

原创粉丝点击