部分转自http://blog.sina.com.cn/s/blog_6d802e7b0100oy01.html
第九章 数据结构
数据结构基本上就是——它们是可以处理一些 数据 的 结构 。或者说,它们是用来存储一组相关数据的
。
在Python中有三种内建的数据结构——列表、元组和字典
————————————————————————————————————
列表
可以在列表中添加 任何种类的对象 包括数甚至其他列表。
for item in shoplist:
print item,
print item,
在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符。
使用列表的sort方法来对列表排序。需要理解的是,这个方法影响列表本身,而不是返回一个修改后的列表——这与字符串工作的方法不同。这就是我们所说的列表是 可变的 而字符串是 不可变的 。
—————————————————————————————————————
元组
元组不可变。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]
输出:
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
含有单个元素的元组,singleton = (2 , )
元组最通常的用法是用在打印语句中
元组最通常的用法是用在打印语句中
age = 22
name = 'Swaroop'
print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name
输出:
Swaroop is 22 years old
Why is Swaroop playing with that python?
Why is Swaroop playing with that python?
print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。
在大多数时候,你可以只使用%s定制,而让Python来提你处理剩余的事情。这种方法对数同样奏效。print '%s is %s years old' % (name, age)也是对的。
在第二个print语句中,我们使用了一个定制,后面跟着%符号后的单个项目——没有圆括号。这只在字符串中只有一个定制的时候有效。
——————————————————————————————————————————————————————
字典
字典是dict类的实例/对象。
字典类似于通过联系人名字查找地址和联系人详细情况的地址簿,即我们把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。
注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以把不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键。
注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以把不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键。
住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。
ab = { 'Swaroop' : 'swaroopch@byteofpython.info','Larry' : 'larry@wall.org','Matsumoto' : 'matz@ruby-lang.org','Spammer' : 'spammer@hotmail.com'}
print "Swaroop's address is %s" % ab['Swaroop']
# 增加 a key/value pair
ab['Guido'] = 'guido@python.org'
# 删除 a key/value pair
del ab['Spammer']
print '\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
print 'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')这里in ab是指键,不是指值
print "\nGuido's address is %s" % ab['Guido']
输出:
Swaroop's address is swaroopch@byteofpython.info
There are 4 contacts in the address-book
Contact Swaroop at swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org
Guido's address is guido@python.org
There are 4 contacts in the address-book
Contact Swaroop at swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org
Guido's address is guido@python.org
------------------------------
Python的字典的items(), keys(), values()
>>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'world' }
>>> dict.values()
['b', 2, 'world']
>>> dict.keys()
['a', 1, 'hello']
>>> dict.items()
[('a', 'b'), (1, 2), ('hello', 'world')]
>>>
——————————————————————————————————————————————
序列
列表、元组和字符串都是序列,序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。
-------------------------------------------------
切片操作符
在python中的原型是[start:stop:step]即:[开始索引:结束索引:步长值]
步长值:默认是一个接着一个切取,如果为2,则表示进行隔一取一操作。步长值为正时表示从左向右取,如果为负,则表示从右向左取。步长值不能为0
li = [1,2,3,4,5,6,7]
print li[1:] #输出[2,3,4,5,6,7],省略终止索引,表示取起始索引之后的所有值,等效于li[1:len(li)]
print li[:3] #输出[1,2,3],省略起始索引,表示从0开始取,等效于li[0:3]
print li[:] #输出[1,2,3,4,5,6,7],等效于li[0:len(li):1]
print li[::] #输出[1,2,3,4,5,6,7],等效于li[0:len(li):1]
print li[::-1] #输出[7,6,5,4,3,2,1],省略起始索引、终止索引,步长值为-1,表示反向获取
步长值:默认是一个接着一个切取,如果为2,则表示进行隔一取一操作。步长值为正时表示从左向右取,如果为负,则表示从右向左取。步长值不能为0
li = [1,2,3,4,5,6,7]
print li[1:] #输出[2,3,4,5,6,7],省略终止索引,表示取起始索引之后的所有值,等效于li[1:len(li)]
print li[:3] #输出[1,2,3],省略起始索引,表示从0开始取,等效于li[0:3]
print li[:] #输出[1,2,3,4,5,6,7],等效于li[0:len(li):1]
print li[::] #输出[1,2,3,4,5,6,7],等效于li[0:len(li):1]
print li[::-1] #输出[7,6,5,4,3,2,1],省略起始索引、终止索引,步长值为-1,表示反向获取
-----------------------------------------------------
序列的神奇之处在于你可以用相同的方法访问元组、列表和字符串。
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]
输出:
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
>>> cd=(1,2,3)
>>> cd[::]
(1, 2, 3)
>>> cd[1:2]#注意下面结果还保留一个逗号。
(2,)
>>> cd[:2]
(1, 2)
>>>
——————————————————————————————————————————————
引用
创建一个对象并给它赋一个变量的时候,这个变量仅仅 引用 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the same object!
print id(shoplist)
print id(mylist)
print 'Copy by making a full slice'
mylist = shoplist[:] # make a copy by doing a full slice
print id(shoplist)
print id(mylist)
# notice that now the two lists are different
输出:
46930760
46930760
Copy by making a full slice
46930760
40948808
46930760
Copy by making a full slice
46930760
40948808
记住列表的赋值语句不创建拷贝。得使用切片操作符来建立序列的拷贝
————————————————————————————————————————————
更多字符串的方法
name = 'Swaroop' # This is a string object
if name.startswith('Swa'):#startwith方法是用来测试字符串是否以给定字符串开始。
print 'Yes, the string starts with "Swa"'
if 'a' in name:#in操作符用来检验一个给定字符串是否为另一个字符串的一部分。
print 'Yes, it contains the string "a"'
if name.find('war') != -1:#find方法用来找出给定字符串在另一个字符串中的位置,或者返回-1以表示找不到子字符串。
print 'Yes, it contains the string "war"'
delimiter = '_*_'#str类也有以一个作为分隔符的字符串join序列的项目的整洁的方法,它返回一个生成的大字符串。
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)
输出:
Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China
-------------------------------------
string.join()
>>> a="abcd"
>>> ",".join(a)
'a,b,c,d'
>>> ",".join(['a','b','c'])
'a,b,c'
>>> ",".join(('a','b','c'))
'a,b,c'
>>> ",".join({'a':1,'b':2,'c':3})
'a,c,b'
————————————————————————————————————————————