Jafe Lee

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            49 隨筆 :: 0 文章 :: 24 評論 :: 0 Trackbacks
          Example 3.1 Defining a Dictionary
          1、每一個dictionary中的每一個item就是一對值:key-value。可以通過key引用value,但不能通過value引用key。
          2、每一個key是大小寫敏感的
          3、給一個dictionary添加一個新item:dictName[newKey] = newValue。如果newKey存在,則newValue會覆蓋原來的value。這也表明添加、修改item用的是相同的語法。
          4、value的類型可以是任意的,同一個dictionary中的value的類型并不需要一致。key的類型必須是所有不可變的類型,tuple有時也可以作為key,這時候tuple不能包含可變類型,不管是直接包含還是間接包含。
          5、del D[k]刪除一個key是k的item。
          6、操作 k in D 可以用來檢查key k 是否包含在D的所有keys中
          7
          Dictionary object methods

          Method

          Description

          Nonmutating methods

          ?

          D.copy( )

          Returns a shallow copy of the dictionary (a copy whose items are the same objects as D's, not copies thereof)

          D.has_key(k)

          Returns TRue if k is a key in D; otherwise, returns False, just like kinD

          D.items( )

          Returns a new list with all items (key/value pairs) in D

          D.keys( )

          Returns a new list with all keys in D

          D.values( )

          Returns a new list with all values in D

          D.iteritems( )

          Returns an iterator on all items (key/value pairs) in D

          D.iterkeys( )

          Returns an iterator on all keys in D

          D.itervalues( )

          Returns an iterator on all values in D

          D.get(k[,x])

          Returns D[k] if k is a key in D; otherwise, returns x (or None, if x is not given)

          Mutating methods

          ?

          D.clear( )

          Removes all items from D, leaving D empty

          D.update(D1)

          For each k in D1, sets D[k] equal to D1[k]

          D.setdefault(k[,x])

          Returns D[k] if k is a key in D; otherwise, sets D[k] equal to x and returns x

          D.pop(k[,x])

          Removes and returns D[k] if k is a key in D; otherwise, returns x (or raises an exception if x is not given)

          D.popitem( )

          Removes and returns an arbitrary item (key/value pair)



          posted on 2007-08-26 11:31 Jafe Lee 閱讀(257) 評論(0)  編輯  收藏 所屬分類: Book Notes--Dive Into Python
          主站蜘蛛池模板: 铜梁县| 特克斯县| 银川市| 辰溪县| 通州区| 班玛县| 清新县| 新平| 靖宇县| 辉县市| 固原市| 临夏县| 宁安市| 蒙城县| 商水县| 汶川县| 确山县| 兴化市| 乾安县| 博客| 石阡县| 麻江县| 龙岩市| 新绛县| 织金县| 江山市| 清河县| 南澳县| 武城县| 通化县| 武冈市| 曲沃县| 太康县| 牙克石市| 合山市| 定西市| 永清县| 乐业县| 喀什市| 邵阳县| 清远市|