1、字典
創(chuàng)建的方法
a = dict(one = 1, two = 2, three = 3)
b = {'one' : 1, 'two' : 2, 'three' : 3}
c = dict(zip(['one', 'two', 'three'], [1,2,3]))
d = dict([('two',2),('one', 1),('three',3)])
e = dict({'three' : 3, 'one' : 1,'two' : 2})
各種內(nèi)置方法
- fromkeys()創(chuàng)建并返回一個(gè)新字典
>>> dict.fromkeys((1,2,3) , ("one", "two", "three"))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
- key()用于返回字典中的鍵蝠嘉,values() 用于返回字典中的所有值、items() 返回所有鍵值對(duì)
- get(鍵值)可以得到對(duì)應(yīng)的值杯巨,如果沒(méi)有蚤告,返回None
- clear() 清空內(nèi)容
- in 或者 not in 可以判斷一個(gè)鍵是否在字典中
- copy() 復(fù)制字典
- pop() 彈出對(duì)應(yīng)的值,popitem() 彈出一個(gè)項(xiàng)
>>>a=dict.fromkeys((1,2,3) , ("one", "two", "three"))
>>>a
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> a.pop(2)
('one', 'two', 'three')
>>> a
{1: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> a.popitem()
(3, ('one', 'two', 'three'))
>>> a
{1: ('one', 'two', 'three')}
>>>
>>> a = {"白":'白',"xiao":'白',"小白":'白'} # 更新字典
>>> a.update(小白= '5')
>>> 5
2服爷、集合