1. 程式人生 > >Python dict get items pop update

Python dict get items pop update

OS pytho 方法 shu none get 存在 python AI

一、get方法

  dict = {‘k1‘:1,‘k2‘:2}

  dict.get(‘k1‘)

  1

  dict.get(‘k2‘)

  2

  dict.get(‘k3‘)

  None

  dict.get(‘k3‘,‘wohaoshuai‘)

  wohaoshuai

  (如果k3不存在那麽就設置為wohaoshuai)

二、items

  dict.items()

  dict_items([(‘a‘, 1), (‘b‘, 2)])

三、pop

  dict.pop(‘k1‘)

  dict

  {‘k2‘:2}

四、update

  d2 = {‘k3‘:3}

  dict.update(d2)

  dict

  {‘k1‘:1,‘k2‘:2,‘k3‘:3}

  

Python dict get items pop update