免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
《笨辦法學(xué)Python》 第38課手記

原創(chuàng) 從流域到海域 最后發(fā)布于2016-02-13 10:53:01 

發(fā)布于2016-02-13 10:53:01
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。

《笨辦法學(xué)Python》 第38課手記

注意這是第三版的《笨辦法學(xué)Python》的內(nèi)容,我后來發(fā)現(xiàn)第三版存在很大的問題,就放棄了第三版開始使用第四版,第四版的第38課是讀代碼,這里保留之前的手記,因?yàn)樗鼈兪怯袃r(jià)值。

不在寫第四課的手記,因?yàn)槭亲x代碼課。但是之后的課程手記全部是針對第四版的,第三版棄之不用了。

本節(jié)課內(nèi)容較多,可以慢慢理解,雖然標(biāo)題是列表,但實(shí)際其實(shí)是一種叫做字典(dict)的數(shù)據(jù)結(jié)構(gòu)。

列表是有序排列的一些物件,而字典是將一些物件(keys)對應(yīng)到另外一些物件(values) 的數(shù)據(jù)結(jié)構(gòu)。這句話出自39課的常見問題解答。

原代碼如下(縮進(jìn)統(tǒng)一使用四個(gè)空格):

# create a mapping of state to abbreviationstates = {    'Oregon': 'OR',    'Florida': 'FL',    'California': 'CA',    'New York': 'NY',    'Michigan': 'MI',   }# create a basic set of states and some cities in themcities = {    'CA': 'San Francisco',    'MI': 'Detroit',    'FL': 'Jacksonville'}# add some more citiescities['NY'] = 'New York'cities['OR'] = 'Portland'# print out some citiesprint '-' * 10print "NY State has: ", cities['NY']print "OR State has: ", cities['OR']# print some statesprint '-' * 10print "Michigan's abbreviation is: ", states['Michigan']print "Florida's abbreviation is: ", states['Florida']# do it by using the states then cities dictprint '-' * 10print "Michigan has: ", cities[states['Michigan']]print "Florida has: ", cities[states['Florida']]# print every state abbreviationprint '-' * 10for state, abbrev in states.items():    print "%s is abbreviated %s" % (state, abbrev)# print every city in stateprint '-' * 10for abbrev, city in cities.items():    print "%s has the city %s" % (abbrev, city)# now do both at the same timeprint '-' * 10for state, abbrev in states.items():    print "%s state is abbreviated %s and has city %s" % (        state, abbrev, cities[abbrev])print '-' * 10# safely get a abbreviation by state that might not be therestate = states.get('Texas', None)if not state:    print "Sorry, no Texas."# get a city with a default valuescity = cities.get('TX', 'Does Not Exist')print "The city for the state 'TX' is: %s" % city

結(jié)果如下:

一塊一塊地來解釋吧。

首先定義了字典,這里有兩種定義(事實(shí)上第二種是在添加字典中的元素)的方式。


第一種:
states = {
‘Oregon’: ‘OR’,
‘Florida’: ‘FL’,
‘California’: ‘CA’,
‘New York’: ‘NY’,
‘Michigan’: ‘MI’,
}
第二種:
cities[‘NY’] = ‘New York’
cities[‘OR’] = ‘Portland’

而在使用時(shí),以states[‘縮寫’]的形式即可表示相應(yīng)的字符串,這比數(shù)組強(qiáng)大的多,數(shù)組只能以基數(shù)來區(qū)分其中的元素,列表以縮寫來區(qū)分,顯然方便的多,使用時(shí)不再要求你記住數(shù)值,縮寫是你在學(xué)英語時(shí)以及記住的東西。


print ‘-’ * 10
print “Michigan has: “, cities[states[‘Michigan’]]
print “Florida has: “, cities[states[‘Florida’]]

這里是嵌套調(diào)用,因?yàn)閟tates和cities是在定義時(shí)就是嵌套定義的,所以可以嵌套使用,請記住這個(gè)用法。


print ‘-’ * 10
for state, abbrev in states.items():
print “%s is abbreviated %s” % (state, abbrev)

abbrev也是一個(gè)關(guān)鍵字,是指列表中元素的縮寫。states.item()會(huì)遍歷states里面的所有內(nèi)容。


state = states.get(‘Texas’, None)
這里涉及到get函數(shù)。

描述:
Python 字典(Dictionary) get() 函數(shù)返回指定鍵的值,如果值不在字典中返回默認(rèn)值。

語法:

dict.get(key, default=None)

參數(shù):
key – 字典中要查找的鍵。
default – 如果指定鍵的值不存在時(shí),返回該默認(rèn)值值。

返回值:
返回指定鍵的值,如果值不在字典中返回默認(rèn)值None。None是一個(gè)邏輯值,表示為假。所以if語句滿足運(yùn)行的條件,而最后一塊代碼中變量city中儲(chǔ)存的值不是一個(gè)邏輯值而是字符串。

本節(jié)課涉及的知識(shí)

其實(shí)本節(jié)課這種類型的列表變量還有專門的名字叫做字典(dict),字典由鍵和值組成,鍵是數(shù)據(jù)庫里面的鍵(key),相當(dāng)于我們?nèi)粘I钪凶值涞捻摯a,是一種索引或者說地址,每一個(gè)鍵都對應(yīng)一個(gè)值。

OperationResult
len(a)the number of items in a 得到字典中元素的個(gè)數(shù)
a[k]the item of a with key k 取得鍵K所對應(yīng)的值
a[k] = vset a[k] to v 設(shè)定鍵k所對應(yīng)的值成為v
del a[k]remove a[k] from a 從字典中刪除鍵為k的元素
a.clear()remove all items from a 清空整個(gè)字典
a.copy()a (shallow) copy of a 得到字典副本
k in aTrue if a has a key k, else False 字典中存在鍵k則為返回True,沒有則返回False
k not in aEquivalent to not k in a 字典中不存在鍵k則為返回true,反之返回False
a.has_key(k)Equivalent to k in a, use that form in new code 等價(jià)于k in a
a.items()a copy of a’s list of (key, value) pairs 得到一個(gè)鍵值的list
a.keys()a copy of a’s list of keys 得到鍵的list
a.update([b])updates (and overwrites) key/value pairs from b 從b字典中更新a字典,如果鍵相同則更新,a中不存在則追加
a.fromkeys(seq[, value])Creates a new dictionary with keys from seq and values set to value 創(chuàng)建一個(gè)新的字典,鍵來自seq,值對應(yīng)鍵對應(yīng)的值
a.values()a copy of a’s list of values 得到字典值的副本
a.get(k[, x])a[k] if k in a, else x 得到a[k],若存在返回x
a.setdefault(k[, x])a[k] if k in a, else x (also setting it) 得到a[k],若不存在返回x,并設(shè)定為x
a.pop(k[, x])a[k] if k in a, else x (and remove k) 彈出a[k],若不存在則返回x,同時(shí)將刪除k鍵
a.popitem()remove and return an arbitrary (key, value) pair 彈出a中對象的鍵和值,并刪除彈出的鍵和值
a.iteritems()return an iterator over (key, value) pairs 返回a中所有對象(鍵和值)
a.iterkeys()return an iterator over the mapping’s keys 返回a中所有鍵(索引)
a.itervalues()return an iterator over the mapping’s values 返回a中所有值

建議每天看一遍,一個(gè)星期之后就能牢記于心。也可以先記住本機(jī)課涉及的內(nèi)容,碰到字典再來翻這一節(jié)課。


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Python數(shù)據(jù)分析、挖掘與可視化(慕課版)電子教案(參考)
學(xué)好Python每天只需5分鐘,掌握17個(gè)冷門技巧,敲代碼速度倍增
Python入門教程,30分鐘玩轉(zhuǎn)Python編程!
Python基礎(chǔ)語法14個(gè)知識(shí)點(diǎn)大串講
30個(gè)Python程序員需要知道的編程技巧,可以讓你的工作事半功倍!
Python打牢基礎(chǔ),從22個(gè)語法開始!
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服