<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 習題 40: 字典, 可愛的字典 接下來我要教你另外一種讓你傷腦筋的容器型數據結構,因為一旦你學會這種容器,你將擁有超酷的能力。這是最有用的容器:字典(dictionary)。 Python 將這種數據類型叫做 “dict”,有的語言里它的名稱是 “hash”。這兩種名字我都會用到,不過這并不重要,重要的是它們和列表的區別。你看,針對列表你可以做這樣的事情: ~~~ >>> things = ['a', 'b', 'c', 'd'] >>> print things[1] b >>> things[1] = 'z' >>> print things[1] z >>> print things ['a', 'z', 'c', 'd'] >>> ~~~ 你可以使用數字作為列表的索引,也就是你可以通過數字找到列表中的元素。而 dict 所作的,是讓你可以通過任何東西找到元素,不只是數字。是的,字典可以將一個物件和另外一個東西關聯,不管它們的類型是什么,我們來看看: ~~~ >>> 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 >>> ~~~ 你將看到除了通過數字以外,我們還可以用字符串來從字典中獲取 stuff ,我們還可以用字符串來往字典中添加元素。當然它支持的不只有字符串,我們還可以做這樣的事情: ~~~ >>> 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} >>> ~~~ 在這里我使用了兩個數字。其實我可以使用任何東西,不過這么說并不準確,不過你先這么理解就行了。 當然了,一個只能放東西進去的字典是沒啥意思的,所以我們還要有刪除物件的方法,也就是使用 del 這個關鍵字: ~~~ >>> del stuff['city'] >>> del stuff[1] >>> del stuff[2] >>> stuff {'name': 'Zed', 'age': 36, 'height': 74} >>> ~~~ 接下來我們要做一個練習,你必須非常仔細,我要求你將這個練習寫下來,然后試著弄懂它做了些什么。這個練習很有趣,做完以后你可能會有豁然開朗的感覺。 <table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 1&#13; 2&#13; 3&#13; 4&#13; 5&#13; 6&#13; 7&#13; 8&#13; 9&#13; 10&#13; 11&#13; 12&#13; 13&#13; 14&#13; 15&#13; 16&#13; 17&#13; 18&#13; 19</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>class Song(object):&#13; &#13; def __init__(self, lyrics):&#13; self.lyrics = lyrics&#13; &#13; def sing_me_a_song(self):&#13; for line in self.lyrics:&#13; print line&#13; &#13; happy_bday = Song(["Happy birthday to you",&#13; "I don't want to get sued",&#13; "So I'll stop right there"])&#13; &#13; bulls_on_parade = Song(["They rally around the family",&#13; "With pockets full of shells"])&#13; &#13; happy_bday.sing_me_a_song()&#13; &#13; bulls_on_parade.sing_me_a_song()&#13; </pre> </div> </td> </tr></tbody></table> Warning 注意到我用了 themap 而不是 map 了吧?這是因為 Python 已經有一個函數稱作 map 了,所以如果你用 map 做變量名,你后面可能會碰到問題。 ### 你應該看到的結果 ~~~ Happy birthday to you I don't want to get sued So I'll stop right there They rally around the family With pockets full of shells ~~~ ### 加分習題 1. 在 Python 文檔中找到 dictionary (又被稱作 dicts, dict)的相關的內容,學著對 dict 做更多的操作。 1. 找出一些 dict 無法做到的事情。例如比較重要的一個就是 dict 的內容是無序的,你可以檢查一下看看是否真是這樣。 1. 試著把 for-loop 執行到 dict 上面,然后試著在 for-loop 中使用 dict 的 items() 函數,看看會有什么樣的結果。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看