<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                字典的用法,dict的查找特別快捷。如果我們的項目經常用到搜索某些數據,最好用dict類型。 ### 認識dict[?](http://uliweb.clkg.org/tutorial/view_chapter/176#title_0-0-1) ~~~ peter@智普教育:~$ python Python 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a={"name":"jike","age":30,"job":"python優秀工程師","level":"智普小天才"} >>> print a["level"] 智普小天才 >>> print a["age"] 30 ~~~ 這就是字典,今年30歲的python工程師是一個天才。 和數據庫結合非常完美。制作一個電話聯系人也很實用。 上面的"name","age"等叫做key; "jike",30,"智普小天才",叫做value。 獲取所有的keys。 ~~~ >>> a={"name":"jike","age":30,"job":"python優秀工程師","level":"智普小天才"} >>> b = a.keys() >>> print b ['job', 'age', 'name', 'level'] >>> type(b) <type 'list'> >>> c = a.values() >>> print c ['python\xe4\xbc\x98\xe7\xa7\x80\xe5\xb7\xa5\xe7\xa8\x8b\xe5\xb8\x88', 30, 'jike', '\xe6\x99\xba\xe6\x99\xae\xe5\xb0\x8f\xe5\xa4\xa9\xe6\x89\x8d'] >>> type(c) <type 'list'> >>> ~~~ 上面的代碼,你看懂了嗎? 請解釋一下。 注意:上面print c的時候有些亂碼是python控制臺的原因。 ### 給dict添加內容[?](http://uliweb.clkg.org/tutorial/view_chapter/176#title_0-0-2) ~~~ a={} a['email']='cool@jeapedu.com' ~~~ 這樣做可以嗎?請試試。 list可以做如下動作嗎?為什么? ~~~ list=[] list[5]=10 ~~~ ### 刪除操作[?](http://uliweb.clkg.org/tutorial/view_chapter/176#title_0-0-3) ~~~ peter@智普教育:~$ python Python 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a={"name":"jike","age":30,"job":"python優秀工程師","level":"智普小天才"} >>> for b in a: print b ... job age name level >>> a.pop("name") 'jike' >>> print a {'job': 'python\xe4\xbc\x98\xe7\xa7\x80\xe5\xb7\xa5\xe7\xa8\x8b\xe5\xb8\x88', 'age': 30, 'level': '\xe6\x99\xba\xe6\x99\xae\xe5\xb0\x8f\xe5\xa4\xa9\xe6\x89\x8d'} >>> >>> del a["job"] >>> print a {'age': 30, 'level': '\xe6\x99\xba\xe6\x99\xae\xe5\xb0\x8f\xe5\xa4\xa9\xe6\x89\x8d'} ~~~ ### 如何判斷key是否存在?[?](http://uliweb.clkg.org/tutorial/view_chapter/176#title_0-0-4) ~~~ >>> a["age"] 30 >>> a["name"] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'name' ~~~ 直接用會出錯。 用for key in a: ? 還有更好的方法嗎? 答案 [code-comment](http://uliweb.clkg.org/wiki/code-comment)?: n1761:a.has_key("name") 獲取鍵值的方法 ~~~ a.get("age") a["age"] ~~~ 區別是什么? a.get比a["name"]更安全,如果key不存在。get函數返回空None,但不會出錯。 update函數的用法 ~~~ >>> print a {'age': 30, 'level': '\xe6\x99\xba\xe6\x99\xae\xe5\xb0\x8f\xe5\xa4\xa9\xe6\x89\x8d'} >>> a1={"hello":1} >>> a.update(a1) >>> print a {'age': 30, 'hello': 1, 'level': '\xe6\x99\xba\xe6\x99\xae\xe5\xb0\x8f\xe5\xa4\xa9\xe6\x89\x8d'} ~~~ ### 獲取key,value的另一個方法 ~~~ In [4]: d.items() Out[4]: [(1, 'a'), (2, 'b'), (3, 'c')] ~~~
                  <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>

                              哎呀哎呀视频在线观看