简明python教程五----数据结构
python中有三種內(nèi)建的數(shù)據(jù)結(jié)構(gòu):列表、元組和字典
list是處理一組有序項(xiàng)目的數(shù)據(jù)結(jié)構(gòu),即你可以在一個(gè)列表中存儲(chǔ)一個(gè)序列的項(xiàng)目。在python中,每個(gè)項(xiàng)目之間用逗號(hào)分隔。
列表中的項(xiàng)目應(yīng)該包括在方括號(hào)中,這樣python就知道你是在指明一個(gè)列表。一旦你創(chuàng)建了一個(gè)列表,可以添加、刪除或者搜索列表中的項(xiàng)目。
列表是可變的數(shù)據(jù)類(lèi)型,這種類(lèi)型是可以被改變的。
python為list類(lèi)提供了append方法,讓你在列表尾添加一個(gè)項(xiàng)目。
如mylist.append('an item')列表mylist中增加那個(gè)字符串。注意使用點(diǎn)號(hào)來(lái)使用對(duì)象的方法。
#!/usr/bin/python #Filename:using_list.py#This is my shopping list shoplist=['apple','mango','carrot','banana'] print 'I have',len(shoplist),'items to purchase.'print 'These items are:', for item in shoplist:print item,print '\nI also have to buy rice.' shoplist.append('rice') print 'My shopping list is now',shoplistprint 'I will sort my list now' shoplist.sort() print 'sorted shopping list is',shoplistprint 'The first item T will buy is',shoplist[0] olditem=shoplist[0] del shoplist[0] print 'I bought the',olditem print 'My shopping list is now',shoplist結(jié)果:
I have 4 items to purchase. These items are: apple mango carrot banana I also have to buy rice. My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice'] I will sort my list now Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice'] The first item I will buy is apple I bought the apple My shopping list is now ['banana', 'carrot', 'mango', 'rice']shoplist是購(gòu)物列表,在shoplist列表中可以添加任何種類(lèi)的對(duì)象包括數(shù)甚至其他列表。
我們使用列表的sort方法來(lái)對(duì)列表排序。注意:這個(gè)方法影響列表本身,而不是返回一個(gè)修改后的列表。(列表可變而字符串是不可變的)
?
元組
元組和列表十分類(lèi)似。只不過(guò)元組和字符串一樣是不可變的,即你不能修改元組。
元組通過(guò)圓括號(hào)中用逗號(hào)分隔的項(xiàng)目定義。元組的值不會(huì)改變
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]輸出結(jié)果:
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 penguinlen函數(shù)可以用來(lái)獲取元組的長(zhǎng)度。
通過(guò)一對(duì)方括號(hào)來(lái)指明某個(gè)項(xiàng)目的位置來(lái)訪問(wèn)元組中的項(xiàng)目,稱(chēng)為索引運(yùn)算符,
含有0個(gè)或1個(gè)項(xiàng)目的元組,空的元組由一對(duì)空的圓括號(hào)組成,如myempty=()
含有單個(gè)元素的元組:你必須在第一個(gè)(唯一一個(gè))項(xiàng)目后跟一個(gè)逗號(hào),python才能區(qū)分元組和表達(dá)式中一個(gè)帶圓括號(hào)的對(duì)象。如singleton=(2,)。
列表之中的列表不會(huì)失去它的身份,同樣元組中的元組,或列表中的元組,他們就是使用一個(gè)對(duì)象存儲(chǔ)的對(duì)象。
?
元組與打印語(yǔ)句
age = 22 name = 'Swaroop'print '%s is %d years old'%(name,age) print 'why is %s playing with that python?'%nameprint語(yǔ)句可以使用跟著%符號(hào)的項(xiàng)目元組的字符串。這些字符串具備定制的功能。
定制可以是%s表示字符串或%d表示整數(shù)。
在第二個(gè)print語(yǔ)句中,我們使用了一個(gè)定制,,后面跟著%符號(hào)后的單個(gè)項(xiàng)目----沒(méi)有圓括號(hào)。這只在字符串中只有一個(gè)定制的時(shí)候有效
?
字典
字典類(lèi)似于你通過(guò)聯(lián)系人名字查找地址和聯(lián)系人詳細(xì)情況的地址簿。我們把鍵(名字)值(詳細(xì)情況)聯(lián)系在一起。
注意:鍵必須唯一。
只能使用不可變的對(duì)象(比如字符串)來(lái)作為字典的鍵,可以把不可變或可變的對(duì)象作為字典的值。
鍵值對(duì)在字典中標(biāo)記方式:d={key1:value1,key2:value2},注意鍵/值對(duì)用冒號(hào)分隔,而各個(gè)對(duì)用逗號(hào)分隔,所以這些都包括在花括號(hào)中。
記住字典中的鍵/值對(duì)是沒(méi)有順序的。如果你想要一個(gè)特定的順序,那么你應(yīng)該在使用前自己對(duì)他們排序
字典是dict類(lèi)的實(shí)例/對(duì)象
#'ab' is short for 'a' ddress 'b' ook ab={'Swaroop':'swaroopch@byteofpython.info','Larry':'larry@wall.org','Matsumoto':'matz@ruby-lang.org','Spammer':'spammer@hotmial.com'} print "Swaroop's address is %s"%ab['Swaroop']ab['Guido']='guido@python.org'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:print "\nGuido's address is %s"%ab['Guido']?
序列
列表、元組和字符串都是序列,序列的兩個(gè)主要特點(diǎn)是索引操作符和切片操作符
切片操作符:讓我們獲取序列的一個(gè)切片,即一部分序列。
shoplist = ['apple','mango','carrot','banana'] 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]print "Item 1 to 3 is",shoplist[1:3] print "Item 2 to end is",shoplist[2:] print "Item 1 to -1 is",shoplist[1:-1] print "Item start to end is",shoplist[:]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[:]結(jié)果:
Item 0 is apple Item 1 is mango Item 2 is carrot Item 3 is banana Item -1 is banana Item -2 is carrot Item 1 to 3 is ['mango', 'carrot'] Item 2 to end is ['carrot', 'banana'] Item 1 to -1 is ['mango', 'carrot'] Item start to end is ['apple', 'mango', 'carrot', 'banana'] characters 1 to 3 is wa characters 2 to end is aroop characters 1 to -1 is waroo characters start to end is swaroop使用索引來(lái)取得序列中的單個(gè)項(xiàng)目,例如shoplist[1]
索引同樣可以是負(fù)數(shù),位置從序列尾開(kāi)始計(jì)算的。因此shoplist[-1]表示序列的最后一個(gè)。
切片操作符是序列名后跟一個(gè)方括號(hào),方括號(hào)中有一對(duì)可選的數(shù)字,并且冒號(hào)分隔。記住數(shù)時(shí)可選的,而冒號(hào)是必須的。
切片操作符的第一個(gè)數(shù)(冒號(hào)前)表示切片開(kāi)始的位置,第二個(gè)數(shù)(冒號(hào)后)表示切片到哪里結(jié)束。
不指明第一個(gè)數(shù),python就從序列首開(kāi)始,不指明第二數(shù),python會(huì)停止在序列尾。
注意:返回的序列從開(kāi)始的位置開(kāi)始,在結(jié)束位置之前結(jié)束。即開(kāi)始位置是包含在序列切片中的,而結(jié)束位置被排斥在切片外。
轉(zhuǎn)載于:https://www.cnblogs.com/Caden-liu8888/p/6383366.html
總結(jié)
以上是生活随笔為你收集整理的简明python教程五----数据结构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 悟饭服务器连接中断,英雄联盟连接服务器失
- 下一篇: 金九银十进大厂必刷的105道Java面试