python list 笔记
#!/usr/bin/env python #coding=utf-8 #设置python编码 from operator import itemgetter import sys ###### class MyList: def __init__(self): self.arr=[] #静态方法 @staticmethod def strToArr(line,split_char): line=line.strip() return line.split(split_char) def __del__(self): del self #添加Val def add(self,val): self.arr.append(val) #删除val def remove(self,val): self.arr.remove(val) #清除 def clear(self): self.arr=[] #验证是否存在 def contains(self,key): return key in self.arr #获取下标Val def getByIndex(self,index): return self.arr[index] def size(self): return len(self.arr) def toString(self): print 'toString->',self.arr print '------使用List API-------' myList=MyList() myList.add(1) myList.add(2) myList.add(3) myList.add(4) myList.add(5) myList.toString() print MyList.strToArr('a-b-c-d-e-f','-') print 'IP:123.123.232.111'.split(':',1) print 'getByIndex 1->', myList.getByIndex(1) print 'contains 1 ->', myList.contains(1) myList.remove(1) print 'size:', myList.size() myList.toString() myList.clear() myList.toString()
$ python test_class.py
toString-> [1, 2, 3, 4, 5]
['a', 'b', 'c', 'd', 'e', 'f']
['IP', '123.123.232.111']
getByIndex 1-> 2
contains 1 -> True
size: 4
toString-> [2, 3, 4, 5]
toString-> []