Data Structure

ds_using_list.py

# This is my shopping list

# list is mutable but string is immutable
# create a list using list = []
# at least has append() sort() method
# want to see more,please help(list) 
# list is a sequence 


shoplist = ['apple','mango','carrot','banana']
print('I have', len(shoplist), 'items to purchase.')
print('These items are:',end=' ')
for item in shoplist:
    print(item, end=' ')

print('\nI also have to buy rice.')
shoplist.append('rice')
print('Now my shopping list is',shoplist)

print('I will sort my list now')
shoplist.sort()
print('Sorted shop list is',shoplist)

print('The first item I will buy is', shoplist[0])

olditem = shoplist[0]   # list取元素是用list[Number]
del shoplist[0]

print('I bought the', olditem)
print('My shop list is now',shoplist)

ds_using_tuple.py

# Tuple 可以近似看做列表,但是元組功能沒有列表豐富
# 元組類似于字符串 所以 Tuple is immutable
# create a tuple tuple = ()
# tuple is a sequence

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 new zoo are', new_zoo)
print('Animals brought from old zoo are',new_zoo[2])    #Tuple取元素也是 tuple[Number]
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]))

# 空的元組由一堆圓括號構(gòu)成 myempty = ()
# 但是只擁有一個項目的元組 必須在第一個(唯一一個)項目后面加上一個逗號
# 這是為了區(qū)別這到底是一個元組 還是 只是一個被括號所環(huán)繞的對象 
# example:singleton = (2,) sigleton is a tuple object
#          singleton = (2) sigleton is a int object

ds_using_dict.py

# 字典將鍵值(Keys)與值(Values)聯(lián)系到一起
# 鍵值必須是唯一的,而且鍵值必須是不可變(immutable)的對象(str)
# 值可以是可變或者不可變的對象
# create a dictionary dict = {key1:value1,key2:value2}
# 成對的鍵值-值不會排序
# 字典是屬于dict類下的實例
# 字典不是序列
# 字典只能根據(jù)key來確定

# ab作為Address Book縮寫
ab = {
    'Swaroop':'swaroop@swaroopch.com',
    'Larry':'larry@wall.org',
    'Matsumoto':'matz@ruby-lang.org',
    'Spammer':'spammer@hotmail.com'
}

# use key to find its value
print("Swaroop's address is",ab['Swaroop'])

# delete a pair of key-value
del ab['Spammer']

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

for name,address in ab.items():     #items() 返回一個包含元組的列表
    print('Contact {} at {}'.format(name,address))

# add a pair of key-value
ab['Guido'] = 'guido@python.org'

if 'Guido' in ab:       #in運算符 可以來檢查某對鍵值-值配對是否存在
    print("\nGuido's address is", ab['Guido'])
    
# 了解更多的有關(guān)dict類的方法 help(dict)
# 其實在定義函數(shù)的參數(shù)列表時躏筏,就是指定了鍵值-值配對

ds_seq.py

# 列表(list)  元組(tuple)  字符串(str)  都可以看做是序列(Sequence)的某種表現(xiàn)形式

# 序列(Sequence)的主要功能是資格測試 也就是 in and not in 和 索引操作
# 序列還有切片(Slicing)運算符  它使我們能夠獲得序列的一部分


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

# Indexing or 'Subscription' operation
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
# 切片中冒號(:)是必須的,但是數(shù)字是可選的
# 第一個數(shù)字是切片開始的位置梢莽,第二個數(shù)字是切片結(jié)束的位置。
# 如果第一個數(shù)字沒有指定,Python將會從序列起始處開始操作
# 如果第二個數(shù)字留空捂贿,Python將會在序列的末尾結(jié)束操作
# 切片操作將包括起始位置捞附,但不包括結(jié)束位置
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])    #不包括-1
print('Item start to end is', shoplist[:])

# Slice on a character
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[:])

# Slice 第三個參數(shù)是Step 默認為1
print(shoplist[::1])
print(shoplist[::2])    #得到第0 2 4 ...個項目
print(shoplist[::3])    #得到第0 3 6 ...個項目
print(shoplist[::-1])   #得到第-1 -2 -3 ...個項目巾乳,即逆序了
print(shoplist[::-2])   #得到第-1 -3 -5 ...個項目
print(shoplist[::-3])   #得到第-1 -4 -7 ...個項目

ds_set.py

# 集合(Set)是無序容器(Collection)
# 當容器中的項目是否存在比起次序,即出現(xiàn)次數(shù)更重要的我們就會使用集合
# 集合可以測試某些對象的資格 檢查他們是否是其他集合的子集
# 找到兩個集合的交集等

bri = set(['brazil','russia','india'])
print('Is India in bri?', 'india' in bri)
print('Is usa in bri?', 'usa' in bri)

bric = bri.copy()
bric.add('China')
print('Is bric the superset of bri?',bric.issuperset(bri))

bri.remove('russia')
# bri & bric 
print("bri and bric 's intersection is", bri.intersection(bric))

ds_reference.py

# 創(chuàng)建了一個對象以后并將其分配給某個變量后 變量會查閱Refer該對象
# 但是變量不會代表對象本身 
# 變量名指向計算機內(nèi)存中存儲了該對象的那一部分
# 即將名稱綁定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)
# 現(xiàn)在他們出現(xiàn)了不同
# 因此如果你希望創(chuàng)建一份序列的副本鸟召,那么必須用切片操作胆绊。
# 僅僅將一個變量名賦予給另一個名稱,那么它們都將查閱同一個對象欧募。

ds_str_method.py

# str更多方法可以help(str)

name = 'Swaroop'

if name.startswith('Swa'):  #startswith()用于查找字符串是否以給定的字符串內(nèi)容開頭
    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 = '_*_'
mylist = ['Brazil','Russia','India','China']
print(delimiter.join(mylist))   #join()用以聯(lián)結(jié)序列中的項目跟继,其中字符串會作為每一項目間的
                                #分隔符种冬,并以此返回一串更大的字符串
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市舔糖,隨后出現(xiàn)的幾起案子碌廓,更是在濱河造成了極大的恐慌,老刑警劉巖剩盒,帶你破解...
    沈念sama閱讀 221,331評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谷婆,死亡現(xiàn)場離奇詭異慨蛙,居然都是意外死亡,警方通過查閱死者的電腦和手機纪挎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,372評論 3 398
  • 文/潘曉璐 我一進店門期贫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人异袄,你說我怎么就攤上這事通砍。” “怎么了烤蜕?”我有些...
    開封第一講書人閱讀 167,755評論 0 360
  • 文/不壞的土叔 我叫張陵封孙,是天一觀的道長。 經(jīng)常有香客問我讽营,道長虎忌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,528評論 1 296
  • 正文 為了忘掉前任橱鹏,我火速辦了婚禮膜蠢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘莉兰。我一直安慰自己挑围,他們只是感情好,可當我...
    茶點故事閱讀 68,526評論 6 397
  • 文/花漫 我一把揭開白布糖荒。 她就那樣靜靜地躺著杉辙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪捶朵。 梳的紋絲不亂的頭發(fā)上蜘矢,一...
    開封第一講書人閱讀 52,166評論 1 308
  • 那天,我揣著相機與錄音泉孩,去河邊找鬼。 笑死并淋,一個胖子當著我的面吹牛寓搬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播县耽,決...
    沈念sama閱讀 40,768評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼句喷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了兔毙?” 一聲冷哼從身側(cè)響起唾琼,我...
    開封第一講書人閱讀 39,664評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎澎剥,沒想到半個月后锡溯,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,205評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,290評論 3 340
  • 正文 我和宋清朗相戀三年祭饭,在試婚紗的時候發(fā)現(xiàn)自己被綠了芜茵。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,435評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡倡蝙,死狀恐怖九串,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情寺鸥,我是刑警寧澤猪钮,帶...
    沈念sama閱讀 36,126評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站胆建,受9級特大地震影響烤低,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜眼坏,卻給世界環(huán)境...
    茶點故事閱讀 41,804評論 3 333
  • 文/蒙蒙 一拂玻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宰译,春花似錦檐蚜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,276評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缀拭,卻和暖如春咳短,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蛛淋。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工咙好, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人褐荷。 一個月前我還...
    沈念sama閱讀 48,818評論 3 376
  • 正文 我出身青樓勾效,卻偏偏與公主長得像,于是被迫代替她去往敵國和親叛甫。 傳聞我的和親對象是個殘疾皇子层宫,可洞房花燭夜當晚...
    茶點故事閱讀 45,442評論 2 359

推薦閱讀更多精彩內(nèi)容