1. 程式人生 > >Byte Of Python(數據結構)

Byte Of Python(數據結構)

元組 麻煩 emp 對象 鍵值 style color index mut

數據結構

數據結構(Data Structure)基本上人如其名——它們只是一種結構,能夠將一些數據聚合在一起。換句話說,它們是用來存儲一系列相關數據的集合。

Python中有四種內置的數據結構——列表(List)、元組(Tuple)、字典(Dictionary)和集合(Set)。

列表

列表是一種用於保存一系列有序項目的集合。列表是一種可變(Mutable)數據類型。

 1 #This is my shopping list
 2 shoplist = [apple, mango, carrot, banana]
 3 
 4 print(I have, len(shoplist), 
items to purchase.) 5 6 print(These items are:, end= ) 7 for item in shoplist: 8 print(item,end= ) 9 10 print(\nI also have to buy rice.) 11 shoplist.append(rice) 12 print(My shopping list is now, shoplist) 13 14 print(I will sort my list now.) 15 shoplist.sort() 16 print(
Sorted shopping list is, shoplist) 17 18 print(The first item I will buy is, shoplist[0]) 19 olditem = shoplist[0] 20 del shoplist[0] 21 print(I bought the, olditem) 22 print(My shopping list is now, shoplist)

元組

元組用於將多個對象保存到一起。元組的一大特征類似於字符串,它們是不可變的,也就是說,你不能編輯或更改元組。

元組通常用於保證某一語句或某一用戶定義的函數可以安全地采用一組數值,即元組內的數值不會改變。

# 我會推薦你總是使用括號
# 來指名元組的開始與結束
# 盡管括號是一個可選選項
# 明了勝過晦澀,顯式優於隱式
zoo = (python, elephant, penguin)
print(Number of animals in the zoo is, len(zoo))

new_zoo = monkey, camel, zoo

print(Number of cages in the new zoo is, len(new_zoo))
print(All animals in the 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])
print(Number of animals in the new zoo is, len(new_zoo)-1 + len(new_zoo[2]))

‘‘‘
一個空的元組由一對圓括號構成,例如myempty = ()
然後一個只擁有一個項目的元組,必須在第一個項目的後面加上一個逗號來指定它,如此一來Python才可以識別出來這個表達式想表達的
究竟是一個元組還是一個被括號所環繞的對象。例如singleton = (2, )
‘‘‘

字典

字典將鍵(Keys)與值(Values)聯立在一起。

註意:

1. 鍵必須是唯一的。

2. 只能使用不可變的對象(如字符串)作為字典的鍵,但是你可以使用可變或不可變的對象作為字典的值。

在字典中,你可以通過使用符號構成d = {key : value1, key2 : value2}這樣的形式,來成對地指定鍵與值。

字典中成對的鍵值不會以任何方式進行排序。如果你希望為它們安排一個特別的次序,只能在使用它們之前自行進行排序。

# ‘ab‘是地址(Address)簿(Book)的縮寫

ab = {
    Swaroop: [email protected],
    Larry: [email protected],
    Masumoto: [email protected],
    Spammer: [email protected]
}

print("Swaroopl‘s address is", ab[Swaroop])

#刪除一對鍵值對
del ab[Spammer]

print(\nThere are {} contacts in the address-book\n.format(len(ab)))

for name, address in ab.items():
    print(Contact {} at {}.format(name,address))

#添加一對鍵值對
ab[Guido] = [email protected]

if Guido in ab:
    print("\nGuido‘s address is", ab[Guido])

序列

列表、元組與字符串都可以看做序列(sequence)的某種表現形式。序列的主要功能是資格測試(Membership Test)(也即是in與not in表達式)和索引操作(Indexing Operations),它們能夠允許我們直接獲取序列中的特定項目。

序列同樣擁有一種切片(Slicing)運算符。

shoplist = [apple, mango, carrot, banana]
name = swaroop

#Indexing or ‘Subscription‘ operation#
#索引或“下標(Subscription)”操作符#
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(Character 0 is, name[0])

#Slicing on a list#
print(Item 1 to 3 is, shoplist[1:3])
print(Item 2 to end is, shoplist[2:])
print(Item 2 to -1 is, shoplist[2:-1])
print(Item 1 to -1 is, shoplist[1:-1])
print(Item Start to end is, shoplist[:])

#從某一字符串中切片#
print(character 1 to 3 is, name[1:3])
print(character 2 to end is, name[2:])
print(character 1 to -1 is, name[1:-1])
print(character start to end is, name[:])

集合

集合(Set)是簡單對象的無序集合(Collection)。當集合中的項目存在與否比起次序或其出現次數更加重要時,我們就會使用集合。

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

print(india in bri)
print(usa in bri)

bric = bri.copy()
bric.add(China)
print(bric.issuperset(bri))
print(bric)

bri.remove(russia)
print(bri & bric)

引用

當你創建了一個對象並將其分配給某個變量時,變量只會查閱(Refer)某個對象,並且它也不會代表本身。也就是說,變量名只是指向你計算機內存中儲存了相應

對象的那一部分。這叫做名稱綁定(Binding)給那個對象。

print(Simple Assignment)
shoplist = [apple, mango, carrot, banana]
#mylist只是指向同一對象的另一種名稱
mylist = shoplist

#我購買了第一項項目,所以我將其從列表中刪除。
del shoplist[0]

print(shoplist is, shoplist)
print(mylist is, mylist)
#註意到shoplist和mylist兩者都
#打印出了其中都沒有apple的同樣的列表,以此我們確認
#它們指向的是同一個對象

print(Copy by making a full slice.)
#通過生成一份完整的切片制作一份列表的副本
mylist = shoplist[:]
#刪除第一個項目
del mylist[0]

print(shoplist is, shoplist)
print(mylist is, mylist)
#註意到現在兩份列表已經不同了

如果你希望創建一份諸如序列等復雜的副本(而非整數這種簡單的對象(Object)),你必須使用切片操作來制作副本。

如果你僅僅是將一個變量名賦予給另一個名稱,那麽它們都將“查閱”同一個對象,如果不小心將造成麻煩。

有關字符串的更多內容

#這是一個字符串對象
‘‘‘
startswith方法用於查找字符串是否以給定的字符串內容開頭
in運算符用以檢查給定的字符串是否是查詢的字符串中一部分
find方法用於定位字符串中給定的子字符串的位置。如果找不到相應子字符串,find會返回-1
str類擁有一個簡潔的方法用以聯結(Join)序列中的項目,其中字符串將會作為每一個項目之間的分隔符,並以此生成並返回一串更大的字符串。
‘‘‘
name = Swaroop if name.startswith(Swa): print(Yes, the string is starts with Swa) if a in name: print(Yes, it contains the string "a") if name.find(war) != -1: print(Yes, it contains the string "war") delimiter = _*_ mylist = [brazil, russia, india] print(delimiter.join(mylist))

Byte Of Python(數據結構)