python之字典

來源:互聯網
上載者:User

標籤:dict   defaults   pass   添加   練習   object   item   default   python   

一、字典的定義

字典是python中唯一的映射類型。

二、字典類型與序列類型的區別:

1.存取和訪問資料的方式不同。
2.序列類型只用數字類型的鍵(從序列的開始按數值順序索引);
3.映射類型可以用其他物件類型作鍵(如:數字、字串、元祖,一般用字串作鍵),和序列類型的鍵不同,映射類型的鍵直4.接或間接地和儲存資料值相關聯。
5.映射類型中的資料是無序排列的。這和序列類型是不一樣的,序列類型是以數值序排列的。
6.映射類型用鍵直接“映射”到值。

字典是Python中最強大的資料類型之一。

 字典的聲明方法(兩種)

三、字典中的方法

def clear(self): # real signature unknown; restored from __doc__
"""
   D.clear() -> None. Remove all items from D.
清空字典
"""
pass

eg:

 



def copy(self): # real signature unknown; restored from __doc__
"""
    D.copy() -> a shallow copy of D
    字典的淺拷貝
  """
pass

@staticmethod # known case
def fromkeys(*args, **kwargs): # real signature unknown
"""
Returns a new dict with keys from iterable and values equal to value.
建立並返回一個新字典,以seq中的元素做該字典的鍵,val做該字典中所有鍵對應的初始值(預設為None)
"""
pass
eg:

 



def get(self, k, d=None): # real signature unknown; restored from __doc__
"""
    D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
    返回字典中key對應的值,若key不存在字典中,則返回default的值(default預設為None)
  """
pass
eg:

 



def items(self): # real signature unknown; restored from __doc__
"""
    D.items() -> a set-like object providing a view on D‘s items
    返回一個包含所有(鍵,值)元祖的列表
  """
pass
eg:

 



def keys(self): # real signature unknown; restored from __doc__
"""
    D.keys() -> a set-like object providing a view on D‘s keys
    返回一個包含字典所有KEY的列表

  """
    pass
eg:

 


def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
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
  
彈出指定key值的元素(因為字典是無序的,所以必須指定key值)
"""
pass
eg:

 


def popitem(self): # real signature unknown; restored from __doc__
"""
D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.
  
隨機彈出
"""
pass
eg:

 


def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
"""
    D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
    
但如果字典中不存在Key鍵,由 adict[key] = default 為它賦值
  """
pass

def update(self, E=None, **F): # known special case of dict.update
"""
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]
  
索引值對添加到字典adict中
"""
pass
eg:

 

 
def values(self): # real signature unknown; restored from __doc__
"""
    D.values() -> an object providing a view on D‘s values
    擷取字典中的值
  """
pass
eg:

四、練習題

有如下值的集合【11,22,33,44,55,66,77,88,99,90】,將所有大於66的值儲存至字典的第一個key中,將小於等於66的值儲存至第二個key中,即{‘k1’:大於66,‘k2’:小於66}

方法一:

結果

方法二:

結果:

 

 


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.