最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

習(xí)題 40: 字典, 可愛的字典?

接下來我要教你另外一種讓你傷腦筋的容器型數(shù)據(jù)結(jié)構(gòu),因?yàn)橐坏┠銓W(xué)會(huì)這種容器,你將擁有超酷的能力。這是最有用的容器:字典(dictionary)。

Python 將這種數(shù)據(jù)類型叫做 “dict”,有的語言里它的名稱是 “hash”。這兩種名字我都會(huì)用到,不過這并不重要,重要的是它們和列表的區(qū)別。你看,針對(duì)列表你可以做這樣的事情:

>>> things = ['a', 'b', 'c', 'd']
>>> print things[1]
b
>>> things[1] = 'z'
>>> print things[1]
z
>>> print things
['a', 'z', 'c', 'd']
>>>

你可以使用數(shù)字作為列表的索引,也就是你可以通過數(shù)字找到列表中的元素。而 dict 所作的,是讓你可以通過任何東西找到元素,不只是數(shù)字。是的,字典可以將一個(gè)物件和另外一個(gè)東西關(guān)聯(lián),不管它們的類型是什么,我們來看看:

>>> stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2}
>>> print stuff['name']
Zed
>>> print stuff['age']
36
>>> print stuff['height']
74
>>> stuff['city'] = "San Francisco"
>>> print stuff['city']
San Francisco
>>>

你將看到除了通過數(shù)字以外,我們還可以用字符串來從字典中獲取 stuff ,我們還可以用字符串來往字典中添加元素。當(dāng)然它支持的不只有字符串,我們還可以做這樣的事情:

>>> stuff[1] = "Wow"
>>> stuff[2] = "Neato"
>>> print stuff[1]
Wow
>>> print stuff[2]
Neato
>>> print stuff
{'city': 'San Francisco', 2: 'Neato',
    'name': 'Zed', 1: 'Wow', 'age': 36,
    'height': 74}
>>>

在這里我使用了兩個(gè)數(shù)字。其實(shí)我可以使用任何東西,不過這么說并不準(zhǔn)確,不過你先這么理解就行了。

當(dāng)然了,一個(gè)只能放東西進(jìn)去的字典是沒啥意思的,所以我們還要有刪除物件的方法,也就是使用 del 這個(gè)關(guān)鍵字:

>>> del stuff['city']
>>> del stuff[1]
>>> del stuff[2]
>>> stuff
{'name': 'Zed', 'age': 36, 'height': 74}
>>>

接下來我們要做一個(gè)練習(xí),你必須非常仔細(xì),我要求你將這個(gè)練習(xí)寫下來,然后試著弄懂它做了些什么。這個(gè)練習(xí)很有趣,做完以后你可能會(huì)有豁然開朗的感覺。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cities = {'CA': 'San Francisco', 'MI': 'Detroit',
                     'FL': 'Jacksonville'}

cities['NY'] = 'New York'
cities['OR'] = 'Portland'

def find_city(themap, state):
    if state in themap:
        return themap[state]
    else:
        return "Not found."

# ok pay attention!
cities['_find'] = find_city

while True:
    print "State? (ENTER to quit)",
    state = raw_input("> ")

    if not state: break

    # this line is the most important ever! study!
    city_found = cities['_find'](cities, state)
    print city_found

Warning

注意到我用了 themap 而不是 map 了吧?這是因?yàn)?Python 已經(jīng)有一個(gè)函數(shù)稱作 map 了,所以如果你用 map 做變量名,你后面可能會(huì)碰到問題。

你應(yīng)該看到的結(jié)果?

$ python ex40.py 
State? (ENTER to quit) > CA
San Francisco
State? (ENTER to quit) > FL
Jacksonville
State? (ENTER to quit) > O
Not found.
State? (ENTER to quit) > OR
Portland
State? (ENTER to quit) > VT
Not found.
State? (ENTER to quit) >

加分習(xí)題?

  1. 在 Python 文檔中找到 dictionary (又被稱作 dicts, dict)的相關(guān)的內(nèi)容,學(xué)著對(duì) dict 做更多的操作。
  2. 找出一些 dict 無法做到的事情。例如比較重要的一個(gè)就是 dict 的內(nèi)容是無序的,你可以檢查一下看看是否真是這樣。
  3. 試著把 for-loop 執(zhí)行到 dict 上面,然后試著在 for-loop 中使用 dict 的 items() 函數(shù),看看會(huì)有什么樣的結(jié)果。

Project Versions

Table Of Contents

Previous topic

習(xí)題 39: 列表的操作

Next topic

習(xí)題 41: 來自 Percal 25 號(hào)行星的哥頓人(Gothons)

This Page

桂阳县| 彰化县| 麻江县| 上饶市| 尼玛县| 晋宁县| 积石山| 彩票| 内江市| 福泉市| 汕头市| 白城市| 苗栗县| 肇源县| 东乌| 深圳市| 隆林| 汉源县| 游戏| 莱芜市| 永春县| 枣阳市| 朔州市| 汝南县| 嵊泗县| 巍山| 临夏县| 兴文县| 惠州市| 利辛县| 西华县| 斗六市| 乐亭县| 新竹市| 建瓯市| 大悟县| 资源县| 龙州县| 嘉定区| 南通市| 花垣县|