Python基礎——字典和有序字典

來源:互聯網
上載者:User

標籤:方式   ack   other   對象   empty   pop   src   one   erro   

字典說明:

在 Python 中, 字典 是一系列 鍵 — 值對 。每個鍵都與一個值相關聯,你可以使用鍵來訪問與之相關聯的值。與鍵相關聯的值可以是數字、字串、列表乃至字典。事實上,可將任何 Python 對象用作字典中的值

定義:

可以用如下的幾種方式定義字典:

dic = {‘color‘: ‘green‘, ‘points‘: 5}dic1 = dict(color = ‘green‘, points = 5)dic2 = dict([(‘color‘, ‘green‘), (‘points‘, 5)])

字典推導式可以從任意的索引值運算式中建立字典

dic = {var:var**2 for var in range(2, 11, 2)}print(dic)            # {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
迴圈字典

在字典中迴圈時,關鍵字和對應的值可以使用 items()方法同時解讀出來:

dic = {‘color‘: ‘green‘, ‘points‘: 5}for key, value in dic.items():    print(key, value)# 輸出# color green# points 5

也可以使用keys()方法只取出字典中的鍵,使用values()方法取出字典中的值

方法
    def clear(self): # 無傳回值,刪除字典中所有的項        """ D.clear() -> None.  Remove all items from D. """        pass    def copy(self): # 對字典的淺複製        """ D.copy() -> a shallow copy of D """        pass    def fromkeys(*args, **kwargs): # 函數用於建立一個新字典,以序列seq中元素做字典的鍵,value為字典所有鍵對應的初始值        """ Returns a new dict with keys from iterable and values equal to value. """        pass    def get(self, k, d=None): # 返回給定索引值的值,如果不存在返回None        """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """        pass    def items(self): # 返回鍵——值對列表        """ D.items() -> a set-like object providing a view on D‘s items """        pass    def keys(self): # 返回鍵列表        """ D.keys() -> a set-like object providing a view on D‘s keys """        pass    def pop(self, k, d=None): # 刪除給定的鍵——值對,並返回該鍵所對應的值,如果不存在產生一個KeyError錯誤        """        D.pop(k[,d]) -> v, remove specified key and return the corresponding value.        If key is not found, d is returned if given, otherwise KeyError is raised        """        pass    def popitem(self): # 刪除某個鍵——值對,並該其索引值對返回,隨機刪除,如果空,返回KeyError錯誤        """        D.popitem() -> (k, v), remove and return some (key, value) pair as a        2-tuple; but raise KeyError if D is empty.        """        pass    def setdefault(self, k, d=None): # 如果 key 在 字典中,返回對應的值。如果不在字典中,則插入 key 及設定的預設值 default,並返回 default ,default 預設值為 None。        """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """        pass    def update(self, E=None, **F): # 把字典dict2的鍵/值對更新到dict裡        """        D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.        If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]        If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v        In either case, this is followed by: for k in F:  D[k] = F[k]        """        pass    def values(self): # 返回字典值列表        """ D.values() -> an object providing a view on D‘s values """        pass
View Code

 

Python基礎——字典和有序字典

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.