python内置数据结构简介

Python内置数据结构简介

数据结构是用来存储相关联的一组数据的集合,在Python中内置了几种数据结构,分别是list, tuple,dictionary,set,string
List

list用来存储规则的(ordered)数据集合,我们可用它来存储一系列的数据元素。假设你有一个购物清单用来表示你需要买的物品,你就可以把它们存储到list中,并用"[]"包起来,元素之间使用","隔开。当你创建一个list后,你就可以添加、删除或者查找list中的元素。因为我们能够添加或者删除元素,所以list是可改变的数据类型。
下面的代码展示了list的基本用法:

  1. #Filename: using_list.py   
  2.   
  3. #购物清单   
  4. shoplist = ['apple', 'mango', 'carrot', 'banana']   
  5. #len()计算list的长度
  6. print('I have ', len(shoplist), 'items to pruchase.')  
  7. #打印出购物清单所有的东西
  8. print('These items are:', end=' ')     
  9. for item in shoplist:   
  10.     print(item, end=' ')   
  11. #在购物清单上添加大米 
  12. print('\nI also have to by rice.')   
  13. shoplist.append('rice')   
  14. print('My shopping list is now', shoplist)   
  15. #对购物清单进行排序 
  16. print('I will sort my list now')   
  17. shoplist.sort()   
  18. print('Sorted shopping list is', shoplist)   
  19. 购物清单中的第一样东西(使用index)
  20. print('The first item I will buy is ', shoplist[0])
  21. 删除第一样东西 
  22. olditem = shoplist[0]   
  23. del shoplist[0]   
  24. print('I bought the', olditem)   
  25. print('My shopping list is now', shoplist)  

输出结果:

  1. $ python using_list.py   
  2. I have 4 items to purchase.   
  3. These items are: apple mango carrot banana   
  4. I also have to buy rice.   
  5. My shopping list is now ['apple', 'mango', 'carrot', 'banana',   
  6. 'rice']   
  7. I will sort my list now   
  8. Sorted shopping list is ['apple', 'banana', 'carrot', 'mango',   
  9. 'rice']   
  10. The first item I will buy is apple   
  11. I bought the apple   
  12. My shopping list is now ['banana', 'carrot', 'mango', 'rice']  

执行过程:
程序中使用shoplist来表示购物清单列表list,在shoplist中,我们用字符串存储物品的名称(当然,你可以在list中存储任何类型的数据元素)。然后我们使用for..in 循环来遍历list中的元素,现在你应该明白list其实也是一个序列(sequence)。我们将在下面看到sequence的详细介绍。
接下来,我们使用list对象的append方法向shoplist中添加新的物品元素,然后通过打印出list的所有元素来检查物品是否真的添加到list中。我们使用sort方法来对shoplist中的元素进行了排序。这里的sort方法直接影响了执行该操作的list对象,而不是返回一个新的排序后的list对象——这与string中的sort方法是不同的。这也是为什么list对象是可改变的(mutable)而string是不可改变的(immutable)。最后我们使用del方法删除list对象中的元素。
更多关于list的介绍可通过help(list)来获得。
############################################
Tuple
Tuple(数组)也是用来存储多个对象,它和list类似但tuple是不可改变的(immutable),这是与list最大的不同。tuple在定义时指定其元素,格式为('xxx','xxx'),其中"()"是可选的,但推荐使用"()"把元素括起来。tuple一般用在能够预知tuple的值不会被改变的地方。
下面的代码展示了tuple的简单用法:

  1. #!/usr/bin/python   
  2. # Filename: using_tuple.py   
  3. zoo = ('python', 'elephant', 'penguin') # remember the parentheses are optional   
  4. print('Number of animals in the zoo is', len(zoo))   
  5. new_zoo = ('monkey', 'camel', zoo)   
  6. print('Number of cages in the new zoo is', len(new_zoo))   
  7. print('All animals in new zoo are', new_zoo)   
  8. print('Animals brought from old zoo are', new_zoo[2])   
  9. print('Last animal brought from old zoo is', new_zoo[2][2])   
  10. print('Number of animals in the new zoo is',   
  11. len(new_zoo)-1+len(new_zoo[2])) 

输出结果:

  1. $ python using_tuple.py   
  2. Number of animals in the zoo is 3  
  3. Number of cages in the new zoo is 3  
  4. All animals in new zoo are ('monkey', 'camel', ('python',   
  5. 'elephant', 'penguin'))   
  6. Animals brought from old zoo are ('python', 'elephant', 'penguin')   
  7. Last animal brought from old zoo is penguin   
  8. Number of animals in the new zoo is 5  

执行过程:
变量zoo指向了(refer to)具有若干个元素的一个tuple,我们使用len函数获得了tuple的长度(即其中元素的个数),这意味着tuple也是一种sequence。
现在我们把zoo和一些新的zoo元素保存到一个新的tuple对象new_zoo中,但我们仍然能得到对原来zoo对象的引用,因为把一个tuple嵌入到另一个tuple中并不会丢失其自身的标识。我们通过指定元素的位置([0])来访问tuple中的元素,这和list的用法是一致的。其中“[]”称为索引操作符(indexing operator)。我们可以通过new_zoo[2]来访问new_zoo中的第三个元素,也可以通过new_zoo[2][2]的方式来访问new_zoo中第三个元素的第三个元素(即嵌套tuple或者多维tuple)。
另外,可以通过指定一对空括号"()"来构造一个空的tuple,例如myempty = ()。尽管这样,仅有一个元素的tuple却不是那么简单。在tuple的仅有的一个元素后面,你必须使用","来与Python中的对象表达式相区分,因为Python中的实例化对象的表达式也是使用的"()"。例如:如果你想让一个tuple仅包含一个值为2的元素,你就必须使用这样来指定:singleton = (2,)

#####################################
Dictionary
dictionary更像是一个地址簿(或者通讯录),你能通过名称来找到他的地址或者联系方式信息,就像我们把keys(name)和values(detials)起来一样。但值得注意的是键值(key)必须是唯一的,否则如果两个人具有相同的姓名(键值)的话,我们将不能找到正确的信息。dictionary中的key只能使用不可改变(immutable),而value部分则没这个限制。这也就意味着你应该只使用简单对象类型作为key。
在Python中,使用下面的方式:d ={key1 : value1, key2 : value2}的方式来指定dictionary中的键值对。每一个键值对中用":"分隔键和值,每一对之间使用","来分隔。在最外面使用"{}"把所有键值对括起来。dictionary中的键值对(key-value pairs)之间并不以任何方式排序,如果你想指定一个规则,你必须在使用它们之间对它们进行排序。
你将要使用到的dictionary对象对象都是dict类的实例/对象(instances/objects)
下面的代码展示了dictionary的简单使用:

  1. #!/usr/bin/python   
  2. # Filename: using_dict.py   
  3.   
  4. # 'ab' is short for 'a'ddress 'b'ook   
  5. ab = { 'Swaroop' : 'swaroop@swaroopch.com',   
  6. 'Larry' : 'larry@wall.org',   
  7. 'Matsumoto' : 'matz@ruby-lang.org',   
  8. 'Spammer' : 'spammer@hotmail.com'  
  9. }   
  10. print("Swaroop's address is", ab['Swaroop'])   
  11. # Deleting a key-value pair   
  12. del ab['Spammer']   
  13. print('\nThere are {0} contacts in the address-book\n'.format(len(ab)))   
  14. for name, address in ab.items():   
  15. print('Contact {0} at {1}'.format(name, address))   
  16. # Adding a key-value pair   
  17. ab['Guido'] = 'guido@python.org'  
  18. if 'Guido' in ab: # OR ab.has_key('Guido')   
  19. print("\nGuido's address is", ab['Guido'])  

输出结果:

  1. $ python using_dict.py   
  2. Swaroop's address is swaroop@swaroopch.com   
  3. There are 3 contacts in the address-book   
  4. Contact Swaroop at swaroop@swaroopch.com   
  5. Contact Matsumoto at matz@ruby-lang.org   
  6. Contact Larry at larry@wall.org   
  7. Guido's address is guido@python.org  

执行过程:
我们使用已经讨论过的方式创建了一个dictionary对象ab。然后使用索引操作符"[key]"通过指定的键值来获得具体的键值对。我们同样使用del语句对键值对(key-value pairs)进行删除操作。我们只需简单地指定dictionary对象和"[key]"就可删除key所对应的键值对。删除操作中,我们并不需要知道指定的key值对象的value。
我们通过dictionary对象的items方法获得一个数组(tuple)的list,其中每个tuple包含了items中的每一对值,('key','value')的形式。在for..in循环中我们得到每个键值对并把key和value分别赋值给了name和address,然后输出了这些值。我们也只简单地使用索引操作符"ab['key']"就可以添加新的元素到dictionary对象中。另外我们还通过in操作符或者dictionary对象的has_key方法来判断某个健值是否在给定的dictionary对象中存在。
如果想得到更多关于dictionary的信息,可通过help(dict)获得。

############################
Set  

set是简单对象的无序的集合。当程序中关注于对象是否存在于集合中而不是它出现的顺序或者次数时,你就可以选择使用set。使用set,你可以执行相关的集合操作,例如某个集合是否为其他集合的子集,或者找出两个不同集合的交集等等。
下面的程序给出了这些简单的应用:

  1. >>> bri = set(['brazil', 'russia', 'india'])   
  2. >>> 'india' in bri   
  3. True   
  4. >>> 'usa' in bri   
  5. False   
  6. >>> bric = bri.copy()   
  7. >>> bric.add('china')   
  8. >>> bric.issuperset(bri)   
  9. True   
  10. >>> bri.remove('russia')   
  11. >>> bri & bric # OR bri.  

>>> bri =set(['brazil', 'russia', 'india'])

>>> 'india' in bri

True

>>> 'usa' in bri

False

>>> bric = bri.copy()

>>> bric.add('china')

>>>bric.issuperset(bri)

True

>>>bri.remove('russia')

>>> bri & bric #OR bri.


执行过程:
这个例子只涉及到了数学课程中的基本集合理论,因此很容易看明白的。这里需要注意的是set对象的创建方式。创建set对象需要使用set类来显示指定,如set(['brazil', 'russia', 'india']),其参数为"[]"括起来的对象序列,对象之间使用","隔开。另外程序中使用in操作符判断元素是否存在于集合中,使用了issupperset方法来判断一个集合是否为其他集合的超集(即集合之间的包含关系),使用了&操作符得到两个集合的交集等等。

##########################################
String
在Python中,string同样被看作对象,并通过内置的操作能完成大量与string相关的操作,例如判断某字符串是否为其他子符串的子串,或者拆去字符串两端的空格等等。
在Python中,程序中使用的字符串都是str的对象。下面的例子将演示该类中的一些有用的方法,如果想了解更多的关于str的信息,可以通过help(str)获得。

  1. # Filename: str_methods.py   
  2. name = 'Swaroop' # This is a string object   
  3. if name.startswith('Swa'):   
  4. print('Yes, the string starts with "Swa"')   
  5. if 'a' in name:   
  6. print('Yes, it contains the string "a"')   
  7. if name.find('war') != -1:   
  8. print('Yes, it contains the string "war"')   
  9. delimiter = '_*_'  
  10. mylist = ['Brazil', 'Russia', 'India', 'China']   
  11. print(delimiter.join(mylist))  

输出结果:

  1. $ python str_methods.py   
  2. Yes, the string starts with "Swa"  
  3. Yes, it contains the string "a"  
  4. Yes, it contains the string "war"  
  5. Brazil_*_Russia_*_India_*_China  

执行过程:
在这段程序中,我们看到了一些string相关的方法。例如startswith用来找出某个字符串是否以给定的子符串开头;in操作用来检查一个给定的串是否是指定串的一部分;而find用来找出某字符串在给定串中出现的位置,如果不存在则返回-1。str类还有一个极及小巧的join方法,把来把sequence中的元素通过指定的连接符(delimiter)连接起来,并返回所有元素连接后形成的最长的字符串。


#####################################
Sequences
list
, tuplestring都是sequence
它们最大的特性是具有in 或者 not in表达式用来判断某个元素是否存在于sequence和具有索引操作。索引操作(indexingoperation)允许我们直接从sequence中获取指定的元素。上述的list, tuple和string也都具有另外一个slicing操作,用来从给定的sequence中获得其子sequence。
下面是关于sequence类型的示例代码:

  1. # Filename: seq.py   
  2. shoplist = ['apple', 'mango', 'carrot', 'banana']   
  3. name = 'swaroop'  
  4. # Indexing or 'Subscription' operation   
  5. print('Item 0 is', shoplist[0])   
  6. print('Item 1 is', shoplist[1])   
  7. print('Item 2 is', shoplist[2])   
  8. print('Item 3 is', shoplist[3])   
  9. print('Item -1 is', shoplist[-1])   
  10. print('Item -2 is', shoplist[-2])   
  11. print('Character 0 is', name[0])   
  12. # Slicing on a list   
  13. print('Item 1 to 3 is', shoplist[1:3])   
  14. print('Item 2 to end is', shoplist[2:])   
  15. print('Item 1 to -1 is', shoplist[1:-1])   
  16. print('Item start to end is', shoplist[:])   
  17. # Slicing on a string   
  18. print('characters 1 to 3 is', name[1:3])   
  19. print('characters 2 to end is', name[2:])   
  20. print('characters 1 to -1 is', name[1:-1])   
  21. print('characters start to end is', name[:])  

输出结果:

  1. $ python seq.py   
  2. Item 0 is apple   
  3. Item 1 is mango   
  4. Item 2 is carrot   
  5. Item 3 is banana   
  6. Item -1 is banana   
  7. Item -2 is carrot   
  8. Character 0 is s   
  9. Item 1 to 3 is ['mango', 'carrot']   
  10. Item 2 to end is ['carrot', 'banana']   
  11. Item 1 to -1 is ['mango', 'carrot']   
  12. Item start to end is ['apple', 'mango', 'carrot', 'banana']   
  13. characters 1 to 3 is wa   
  14. characters 2 to end is aroop   
  15. characters 1 to -1 is waroo   
  16. characters start to end is swaroop  

执行流程:
首先我们看到是如何通过索引获得sequence中的单个元素。它还有另外一个名字:查阅操作(subscription operation)。无论何时,你对一个sequence对象通过中括号和数字执行操作时,Python就能获得sequence相应位置上的元素。和其他主流语言一样,Python也是从0开始记数的。所以shoplist[0]获得第一个元素而shoplist[3]则获得了shoplist中的第4个元素。
Python中的索引也能是负数,这时元素的位置从sequence的末尾开始记数。所以shoplist[-1]指向了sequence的最后一个元素,shoplist[-2]得到sequence中倒数第二个元素。slicing操作通过在sequence对象后面加上中括号,及中括号中用':'隔开的数字。这与索引操作非常相似,不同的是这里的数字是可选的,并不强制需要指定。":"前的数字指子串的开始位置,":"后面的数字是子串结束的后一个位置,实际的子串并不包含该位置的元素。如果不指定第一个数字,Python将从原sequence的开始位置开始操作;如果第二个数字为空,Python将在原sequence结束位置终止。记住:子串包含开始位置位不包含结束位置。
所以,shoplist[1:3]将返回从1位置开始,包含2位置的元素但不包含第3个位置上的元素,所以返回了包含两个元素的子序列。类似的,shoplist[:]将返回原来sequence的一个拷贝。
在slicing操作中也可以使用负数。这里的负数的意义和索引操作中的意思相同,因此不做太多解释。例如:shoplist[:-1]将返回不包含最后一个元素的子sequence。
在slicing操作中也可以提供第三个数字(与第二个数字之间同样用":"隔开),这个数字代表slicing操作中的步长(默认值为1),意思是每隔一个步长的位置取元素,作为子requence的下一个位置的元素。下面代码解释了这种用法:

  1. >>> shoplist = ['apple', 'mango', 'carrot', 'banana']   
  2. >>> shoplist[::1]   
  3. ['apple', 'mango', 'carrot', 'banana']   
  4. >>> shoplist[::2]   
  5. ['apple', 'carrot']   
  6. >>> shoplist[::3]   
  7. ['apple', 'banana']   
  8. >>> shoplist[::-1]   
  9. ['banana', 'carrot', 'mango', 'apple']  

>>> shoplist =['apple', 'mango', 'carrot', 'banana']

>>> shoplist[::1]

['apple', 'mango', 'carrot','banana']

>>> shoplist[::2]

['apple', 'carrot']

>>> shoplist[::3]

['apple', 'banana']

>>> shoplist[::-1]

['banana', 'carrot', 'mango','apple']


当步长为2时,我们将获得0,2,...位置上的元素;而步长为3时,我们将得到0,3位置上的元素,依此类推。
sequence中最重要的想法是能使用相同的方式访问tuple, list和string中的元素。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值