Python中有3种内建的数据结构:列表、元组和字典。参考简明Python教程
- 列表
list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个 序列 的项目。假想你有一个购物列表,上面记载着你要买的东西,你就容易理解列表了。只不过在你的购物表上,可能每样东西都独自占有一行,而在Python中,你在每个项目之间用逗号分割。
列表中的项目应该包括在方括号中,这样Python就知道你是在指明一个列表。一旦你创建了一个列表,你可以添加、删除或是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是 可变的 数据类型,即这种类型是可以被改变的。
例:
#!/usr/bin/env python
#coding:utf8
list = ['Linux', 'Nginx', 'MySQL', 'PHP']
print 'These items are:',
for item in list:
print item,
print '\nadd Apache.'
list.append('Apache')
print 'list is now', list
print '\nI will sort my list now'
list.sort()
print 'Sorted list is %s' % list
print '\nThe first item ', list[0]
item0 = list[0]
print 'delete first item'
de