代码改变世界

python数据结构学习记录

2013-03-05 11:28  littlelion  阅读(261)  评论(0编辑  收藏  举报

python有三种数据结构:列表,字典和元组

1.元组

元组通过圆括号来表示,通过逗号来分隔元组中的内容。元组一旦建立就不可修改。下面是元组定义的举例。

#!/bin/python
#using_tuple

zoo = ("pig","cat","dog","lion")
new_zoo = ("bear","panda",zoo)

print "zoo : ",zoo
print "length of tuple zoo : ",len(zoo)
print "new_zoo : ",new_zoo
print "length of new_zoo : ",len(new_zoo)
print "zoo[0] is ",zoo[0]
print "zoo[3] is ",zoo[3]
print "new_zoo[0] is ",new_zoo[0]
print "new_zoo[1] is ",new_zoo[1]
print "new_zoo[2] is ",new_zoo[2]
print "new_zoo[2][2] is ",new_zoo[2][2]

输出结果如下:

zoo :  ('pig', 'cat', 'dog', 'lion')
length of tuple zoo :  4
new_zoo :  ('bear', 'panda', ('pig', 'cat', 'dog', 'lion'))
length of new_zoo :  3
zoo[0] is  pig
zoo[3] is  lion
new_zoo[0] is  bear
new_zoo[1] is  panda
new_zoo[2] is  ('pig', 'cat', 'dog', 'lion')
new_zoo[2][2] is  dog

 2.列表 list

列表用方括号表示,用逗号分割列表元素,列表是可变的数据结构,可以添加,删除和查找列表中的元素(例如购物表单)

列表的使用例子:

#!/bin/python
#using_list
mylist = ['go','python','lua','js']
print "length of list ",len(mylist)
for item in mylist:
        print item

print "add element"
mylist.append('ruby')
print "mylist : ",mylist

print "list[0] : ",mylist[0]
print "delete element"
del mylist[2]
print mylist
mylist.sort()
print mylist

输出:

length of list  4
go
python
lua
js
add element
mylist :  ['go', 'python', 'lua', 'js', 'ruby']
list[0] :  go
delete element
['go', 'python', 'js', 'ruby']
['go', 'js', 'python', 'ruby']

列表是小狮子的语言列表,列表中可以添加任何类型的对象,包括列表本身。方法append表示向列表中添加元素,sort表示对列表中元素排序,排序后再打印列表,列表已经改变。del语句表示删除列表中的元素。

3.字典

姓名(键)——电话号码(值)

键一般就是简单的对象,不可变。值可变。字典用大括号表示:{key1:value1,key2:value2} 

字典的操作举例:

#!/bin/python
#using_dict

mark = {'lion':'100',
        'dog':'98',
        'panda':'99',
        'pig':'96'}
print "mark is ",mark
print "mark[lion] ",mark['lion']
print "add element",
mark['cat'] = '93'
print mark
del mark['dog']
print mark
for name,num in mark.items():
        print 'name %s  num is %s' %(name,num)

if 'lion' in mark:
        print "lion num is ",mark['lion']

输出结果:

mark is  {'lion': '100', 'pig': '96', 'dog': '98', 'panda': '99'}
mark[lion]  100
add element {'lion': '100', 'pig': '96', 'dog': '98', 'cat': '93', 'panda': '99'}
{'lion': '100', 'pig': '96', 'cat': '93', 'panda': '99'}
name lion  num is 100
name pig  num is 96
name cat  num is 93
name panda  num is 99
lion num is  100

元组,列表,字典都是序列。序列的两个主要特点就是索引和切片,索引操作就是从序列中取得一个元素,切片是取得序列的一个部分。

序列操作举例:

#!/bin/python
mylist = ['a','b','c','d','e']
print "mylist ",mylist
print "mylist[0] ",mylist[0]
print "mylist[1] ",mylist[1]
print "mylist[-1] ",mylist[-1]
print "mylist[-2] ",mylist[-2]
print "mylist[0:3] ",mylist[0:3]
print "mylist[1:2] ",mylist[1:2]
print "mylist[:4] ",mylist[:4]
print "mylist[1:] ",mylist[1:]

输出结果:

mylist  ['a', 'b', 'c', 'd', 'e']
mylist[0]  a
mylist[1]  b
mylist[-1]  e
mylist[-2]  d
mylist[0:3]  ['a', 'b', 'c']
mylist[1:2]  ['b']
mylist[:4]  ['a', 'b', 'c', 'd']
mylist[1:]  ['b', 'c', 'd', 'e']

注意:序列的输出是左闭右开的,冒号前不写代表start_index,冒号后不写代表end_index