初始化
例: list = [123, 'abc']
查看某个元素
list[0]
增加元素
list += 'a'
删除元素
del list[0]
下面给出一个对象的例子
class test:
test_num = 0;
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
test.test_num += 1
def show_test_num(self):
print "num is %d" % test.test_num
def show_x_y(self):
print "x is %d and y is %d" %(self.x, self.y)
test1 = test(1, 2)
test2 = test(2, 3)
list = []
list+=[test1]
list+=[test2]
for i in list:
i.show_x_y()
本文介绍了Python中列表的基本操作,包括初始化、查看元素、增加元素及删除元素的方法。此外,还提供了一个类实例化的例子,展示了如何创建类、初始化属性、定义方法,并将类的实例添加到列表中进行遍历。
775

被折叠的 条评论
为什么被折叠?



