简明python教程五----数据结构
python中有三種內建的數據結構:列表、元組和字典
list是處理一組有序項目的數據結構,即你可以在一個列表中存儲一個序列的項目。在python中,每個項目之間用逗號分隔。
列表中的項目應該包括在方括號中,這樣python就知道你是在指明一個列表。一旦你創建了一個列表,可以添加、刪除或者搜索列表中的項目。
列表是可變的數據類型,這種類型是可以被改變的。
python為list類提供了append方法,讓你在列表尾添加一個項目。
如mylist.append('an item')列表mylist中增加那個字符串。注意使用點號來使用對象的方法。
#!/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結果:
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是購物列表,在shoplist列表中可以添加任何種類的對象包括數甚至其他列表。
我們使用列表的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 penguinlen函數可以用來獲取元組的長度。
通過一對方括號來指明某個項目的位置來訪問元組中的項目,稱為索引運算符,
含有0個或1個項目的元組,空的元組由一對空的圓括號組成,如myempty=()
含有單個元素的元組:你必須在第一個(唯一一個)項目后跟一個逗號,python才能區分元組和表達式中一個帶圓括號的對象。如singleton=(2,)。
列表之中的列表不會失去它的身份,同樣元組中的元組,或列表中的元組,他們就是使用一個對象存儲的對象。
?
元組與打印語句
age = 22 name = 'Swaroop'print '%s is %d years old'%(name,age) print 'why is %s playing with that python?'%nameprint語句可以使用跟著%符號的項目元組的字符串。這些字符串具備定制的功能。
定制可以是%s表示字符串或%d表示整數。
在第二個print語句中,我們使用了一個定制,,后面跟著%符號后的單個項目----沒有圓括號。這只在字符串中只有一個定制的時候有效
?
字典
字典類似于你通過聯系人名字查找地址和聯系人詳細情況的地址簿。我們把鍵(名字)值(詳細情況)聯系在一起。
注意:鍵必須唯一。
只能使用不可變的對象(比如字符串)來作為字典的鍵,可以把不可變或可變的對象作為字典的值。
鍵值對在字典中標記方式:d={key1:value1,key2:value2},注意鍵/值對用冒號分隔,而各個對用逗號分隔,所以這些都包括在花括號中。
記住字典中的鍵/值對是沒有順序的。如果你想要一個特定的順序,那么你應該在使用前自己對他們排序
字典是dict類的實例/對象
#'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']?
序列
列表、元組和字符串都是序列,序列的兩個主要特點是索引操作符和切片操作符
切片操作符:讓我們獲取序列的一個切片,即一部分序列。
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[:]結果:
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使用索引來取得序列中的單個項目,例如shoplist[1]
索引同樣可以是負數,位置從序列尾開始計算的。因此shoplist[-1]表示序列的最后一個。
切片操作符是序列名后跟一個方括號,方括號中有一對可選的數字,并且冒號分隔。記住數時可選的,而冒號是必須的。
切片操作符的第一個數(冒號前)表示切片開始的位置,第二個數(冒號后)表示切片到哪里結束。
不指明第一個數,python就從序列首開始,不指明第二數,python會停止在序列尾。
注意:返回的序列從開始的位置開始,在結束位置之前結束。即開始位置是包含在序列切片中的,而結束位置被排斥在切片外。
轉載于:https://www.cnblogs.com/Caden-liu8888/p/6383366.html
總結
以上是生活随笔為你收集整理的简明python教程五----数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 悟饭服务器连接中断,英雄联盟连接服务器失
- 下一篇: websocket python爬虫_p