list用方括号表示,比如
list=['tiger','cat','dog']
通过append()函数,可以向list的末尾增加元素
下面看个例子
suitcase = []
suitcase.append("sunglasses")
suitcase.append("hallo")
suitcase.append('123')
suitcase.append('1.234')
list_length = len(suitcase) # Set this to the length of suitcase
print "There are %d items in the suitcase." % (list_length)
print suitcase
list的切片,就不多写了,其他篇幅里面有
下面写一下list的检索
animals = ["ant", "bat","cat"]
print animals.index("bat")
1
同样,我们也可以插入元素到list里面,如下用insert()
animals.insert(1, "dog")
print animals
animals = ["ant","dog", "bat","cat"]
还有一个例子比较有代表性,如下
animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck") # Use index() to find "duck"
animals.insert(duck_index,'cobra')# Your code here!
print animals # Observe what prints after the insert operation
然后还有删除元素的功能,如下
beatles = ["john","paul","george","ringo","stuart"]
beatles.remove("stuart")
print beatles
>> ["john","paul","george","ringo"]