数据结构

来源:互联网 发布:线切割自动编程软件 编辑:程序博客网 时间:2024/06/12 23:41

数据结构:可以处理一些数据的结构。用来存储一组相关数据的。

python中有三种内建的数据结构-----列表、元组和字典。

------------------------------------->列表list

list是处理一组有序项目的数据结构,即可以在一个列表中存储一个序列的项目,列表中的项目包括在方括号内,

这样python知道指明一个列表,列表时可变的数据类型,创建列表后,可以添加、删除、查找列表中的项目。

列表是使用对象和类的一个例子。当你使用变量i并给它赋值的时候,比如赋整数5,你可以认为你创建了一个类(类型)int的对象(实例)i。

类也有方法,即仅仅为类而定义地函数。仅仅在你有一个该类的对象的时候,你才可以使用这些功能。例如,Python为list类提供了append方法,这个方法让你在列表尾添加一个项目。例如mylist.append('an item')列表mylist中增加那个字符串。注意,使用点号来使用对象的方法。
一个类也有域,它是仅仅为类而定义的变量。仅仅在你有一个该类的对象的时候,你才可以使用这些变量/名称。类也通过点号使用,例如mylist.field。

例:

#!/usr/bin/python


#this is my shopping list
shoplist = ['apple','banana','cherry','carrot']


print 'I have',len(shoplist),'item to purchase'


print 'these items are:',
#for item in shoplist:
# print item,


for i in range(0,len(shoplist)):
print shoplist[i],


#add an item
print '\nI also have to buy rice.'
shoplist.append(1)
print 'My shopping list now is',shoplist


#sort
print 'sort the shoplist'
shoplist.sort()
print 'After sort the shoplist is',shoplist


#delete an item
print 'the first item I need buy is',shoplist[0]
olditem=shoplist[0]
del shoplist[0]
print 'I have bought the ',olditem
print 'My shopping list now is',shoplist

运行输出:

shiyan@ubuntu:~/Desktop/pythonLearn$ ./list.py
I have 4 item to purchase
these items are: apple banana cherry carrot 
I also have to buy rice.
My shopping list now is ['apple', 'banana', 'cherry', 'carrot', 1]
sort the shoplist
After sort the shoplist is [1, 'apple', 'banana', 'carrot', 'cherry']
the first item I need buy is 1
I have bought the  1
My shopping list now is ['apple', 'banana', 'carrot', 'cherry']

注意:可以在列表中添加任何对象,包括数甚至其他列表。

print语句的结尾使用一个逗号来消除每个print语句自动打印的换行符(用空格代替)

------------------------------------------>元组

元组和列表十分类似,但元组和字符串是一样的,不可变的。

元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。

例:

#!/usr/bin/python
zoo=('wolf','dog','cat')
print 'Number of animals in the zoo is',len(zoo)


new_zoo=('monky','panda',zoo)
print 'Number of animals in the new zoo is',len(new_zoo)
print 'All animals in the new zoo are',new_zoo
print 'Animals brount from old zoo are',new_zoo[2]
print 'Last animal brought from old zoo is',new_zoo[2][2]

运行输出:

shiyan@ubuntu:~/Desktop/pythonLearn$ ./tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in the new zoo are ('monky', 'panda', ('wolf', 'dog', 'cat'))
Animals brount from old zoo are ('wolf', 'dog', 'cat')
Last animal brought from old zoo is cat
shiyan@ubuntu:~/Desktop/pythonLearn$ 

元组最通常的用法是用在打印语句中;

例:

#!/usr/bin/python
age=22
name='xiaohong'


print '%s is %d years old' %(name,age)
print 'why is %s play with that python?' %name

运行输出:

shiyan@ubuntu:~/Desktop/pythonLearn$ ./tuple.py
xiaohong is 22 years old
why is xiaohong play with that python?
shiyan@ubuntu:~/Desktop/pythonLearn$

------------------------------------------>字典

字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。

注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键

键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。
记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。字典是dict类的实例/对象。

例:

#!/usr/bin/python


#'mb' is short for 'mailbook'
mb={  'shiyan':'shiyan2016@126.com',
'xiaohong':'xiaohong@163.com',
'meifang':'meifang@126.com',
'jiqing':'jiqing@163.com'
}
print "shiyan's mail is %s" %mb['shiyan']


#add a new key/value pair
mb['shabi']='shabi@python.org'


#delete a key/value pair
del mb['xiaohong']


print '\nThere are %d contacts in the mailbook\n' %len(mb)
for name,mail in mb.items():
print "Contact %s's mail is %s" % (name,mail)

------------------------------------------>字典
if 'meifang' in mb: # or mb.has_key('meifang')
print "meifang's mail is %s" % mb['meifang']


运行输出:

shiyan@ubuntu:~/Desktop/pythonLearn$ ./dict.py 
shiyan's mail is shiyan2016@126.com


There are 4 contacts in the mailbook


Contact shabi's mail is shabi@python.org
Contact meifang's mail is meifang@126.com
Contact shiyan's mail is shiyan2016@126.com
Contact jiqing's mail is jiqing@163.com
meifang's mail is meifang@126.com

------------------------------------------>序列

列表、元组和字符串都是序列序列的两个主要特点是索引和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。

例:

#!/usr/bin/python
shoplist=['apple','banana','carrot','cherry']


#index on a list
print 'Item 0 is',shoplist[0]
print 'Item 1 is',shoplist[1]
print 'Item 2 is',shoplist[2]
print 'Item 3 is',shoplist[3]
print 'Item -1 is',shoplist[-1]
print 'Item -2 is',shoplist[-2]


#slice on a list 
print 'Item 1 to 3 is',shoplist[1:3]
print 'Item 2 to end is',shoplist[2:]
print 'Item start to end is',shoplist[:]
print 'Item 1 to -1 is',shoplist[1:-1]


#slicing on a string
name='snoopy'
print 'characters 1 to 3 is',name[1:3]
print 'characters 1 to -1 is',name[1:-1]
print 'characters 2 to end is',name[2:]

print 'characters start to end is',name[:]

运行输出:

shiyan@ubuntu:~/Desktop/pythonLearn$ ./test.py
Item 0 is apple
Item 1 is banana
Item 2 is carrot
Item 3 is cherry
Item -1 is cherry
Item -2 is carrot
Item 1 to 3 is ['banana', 'carrot']
Item 2 to end is ['carrot', 'cherry']
Item start to end is ['apple', 'banana', 'carrot', 'cherry']
Item 1 to -1 is ['banana', 'carrot']
characters 1 to 3 is no
characters 1 to -1 is noop
characters 2 to end is oopy
characters start to end is snoopy
shiyan@ubuntu:~/Desktop/pythonLearn$ 

注意:索引可以为负数,此时位置是从序列尾开始计算。-1表示末尾最后一个元素

----------------------------------------------------->参考

当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 参考 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。

例:

#!/usr/bin/python
print 'Simple Assignment'
shoplist=['apple','banana','carrot','cherry']
mylist=shoplist #mylist is just another name pointing to the same object


del shoplist[0]
print 'shoplist is',shoplist
print 'mylist is',mylist


#notice that both shoplist and mylist both print the same list without 
#the 'apple' confirming that they point to the same object


print 'copy by making a full slice'
mylist=shoplist[:] #make a copy by doing a full slice
del mylist[0]


print 'shoplist is',shoplist
print 'mylist is ',mylist


#notice now the two list is different


运行输出:

shiyan@ubuntu:~/Desktop/pythonLearn$ ./test.py
Simple Assignment
shoplist is ['banana', 'carrot', 'cherry']
mylist is ['banana', 'carrot', 'cherry']
copy by making a full slice
shoplist is ['banana', 'carrot', 'cherry']
mylist is  ['carrot', 'cherry']
shiyan@ubuntu:~/Desktop/pythonLearn$ 

注意:如果要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单对象),那么必须使用切片操作符来取得

拷贝。如果只是要使用另一个变量名,两个名词都参考同一个对象