Python字典方法总结

1.清空字典中元素清空,dict变为{}

L.clear()-> None.  Remove all items from L

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> L.clear()

>>> print L

{}

2.返回一个字典的浅复制

L.copy()-> a shallow copy of L

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> L.copy()

{‘shaw‘: 23, ‘sam‘: 36,‘eric‘: 40}

3.用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值(默认为“None”)

Lict.fromkeys(S[,v])-> New Lict with keys from S and values equal to v. v defaults toNone.

>>> seq =(‘shaw‘,‘sam‘,‘stiven‘)

>>> name =dict.fromkeys(seq)

>>> print"listone :{}".format(name)

listone :{‘stiven‘: None,‘shaw‘: None, ‘sam‘: None}

>>> name =dict.fromkeys(seq,1000)

>>> print"listone :{}".format(name)

listone :{‘stiven‘: 1000,‘shaw‘: 1000, ‘sam‘: 1000}

4.返回指定键的值,如果值不在字典中返回默认值(None)

D.get(k[,d])-> D[k] if k in D, else d.  d defaultsto None.

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> L.get(‘shaw‘)

23

>>> printL.get(‘stiven‘)

None

5.用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false

L.has_key(k) -> True if D has a key k,else False

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> L.has_key(‘sam‘)

True

>>>L.has_key(‘linux‘)

False

6.以列表的方式返回可遍历的(键, 值) 元组(键值对)

L.items()-> list of D‘s (key, value) pairs, as 2-tuples

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> L.items()

[(‘shaw‘, 23), (‘sam‘, 36),(‘eric‘, 40)]

7.以列表的方式返回一个字典所有的键

L.keys()-> a set-like object providing a view on L‘s keys

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> L.keys()

[‘shaw‘, ‘sam‘, ‘eric‘]

8.删除某个键值对

D.pop(k[,d])-> v, remove specified key and return the corresponding value. If key is notfound, d is returned if given, otherwise KeyError is raised

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> L.pop(‘sam‘)

36

>>> L

{‘shaw‘: 23, ‘eric‘: 40}

9.默认删除字典中第一个键值对

D.popitem()-> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> L.popitem()

(‘shaw‘, 23)

>>> L

{‘sam‘: 36, ‘eric‘: 40}

10. setdefault()方法和get()方法类似,如果键不已经存在于字典中,将会添加键并将值设为默认值(如果dict中已有a,则不会被覆盖)

D.setdefault(k[,d]) ->D.get(k,d), also set D[k]=d if k not in D

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>>L.setdefault(‘stiven‘)

>>> L

{‘stiven‘: None, ‘shaw‘: 23,‘sam‘: 36, ‘eric‘: 40}

>>>L.setdefault(‘mira‘,65)

65

>>> L

{‘stiven‘: None, ‘mira‘: 65,‘shaw‘: 23, ‘sam‘: 36, ‘eric‘: 40}

>>>L.setdefault(‘shaw‘,18)

23

>>> L

{‘stiven‘: None, ‘mira‘: 65,‘shaw‘: 23, ‘sam‘: 36, ‘eric‘: 40}

11.把字典dict2的键/值对更新到dict里

L.update()

>>> L ={‘shaw‘:23,‘sam‘:36,"eric":40}

>>> A ={‘book‘:45,‘apple‘:13}

>>> L.update(A)

>>> L

{‘book‘: 45, ‘apple‘: 13,‘shaw‘: 23, ‘sam‘: 36, ‘eric‘: 40}

12.返回dic所有的值

L.values(…)

>>> L = {‘book‘:45,‘apple‘:13}

>>> L.values()

[45, 13]

时间: 2024-08-03 23:47:13

Python字典方法总结的相关文章

Python字典方法copy()和deepcopy()的区别

1 from copy import deepcopy # import deepcopy模块 2 d = {} 3 d['name'] = ['black', 'guts'] # d = {'name': ['black', 'guts']} 4 c = d.copy() # c = {'name': ['black', 'guts']} 5 dc = deepcopy(d) # dc = {'name': ['black', 'guts']} 6 d['name'].append('whit

Python字典方法

0x01 clear clear方法清除字典中所有的项.这是一个原地操作,无返回值 1 >>> d = {} 2 >>> d['name'] = 'Gumby' 3 >>> d['age'] = 13 4 >>> d 5 {'age': 13, 'name': 'Gumby'} 6 >>> value = d.clear 7 >>> print value 8 <built-in method

Python 字典(Dictionary) get()方法

描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 key -- 字典中要查找的键. default -- 如果指定键的值不存在时,返回该默认值值. 返回值 返回指定键的值,如果值不在字典中返回默认值None. 实例 以下实例展示了 get()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'A

Python字典高级使用方法汇总

Python字典高级使用方法汇总 字典(dictionary)是python中的一种非常灵活和强大的数据结构,可以完成很多操作.本文总结了一些除了基本的初始化.赋值.取值之外的常用的字典使用方法. 字典基础参考: [1]:http://www.w3cschool.cc/python/python-dictionary.html [2]:http://www.111cn.net/phper/python/56355.htm [3]:http://skyfen.iteye.com/blog/5675

Python 字典(Dictionary) setdefault()方法

描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: dict.setdefault(key, default=None) 参数 key -- 查找的键值. default -- 键不存在时,设置的默认键值;存在则不设置. 返回值 该方法没有任何返回值. 实例 以下实例展示了 setdefault()函数的使用方法: #!/usr/bin/pytho

Python 字典(Dictionary) has_key()方法-判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false

描述 Python 字典(Dictionary) has_key() 函数用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false. 语法 has_key()方法语法: dict.has_key(key) 参数 key -- 要在字典中查找的键. 返回值 如果键在字典里返回true,否则返回false. 实例 以下实例展示了 has_key()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} pri

Python 字典(Dictionary) clear()方法-用于删除字典内所有元素

描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素. 语法 clear()方法语法: dict.clear() 参数 NA. 返回值 该函数没有任何返回值. 实例 以下实例展示了 clear()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7}; print "Start Len : %d" % len(dict) dict.clear() print "End L

Python 字典(Dictionary) items()方法-以列表返回可遍历的(键, 值) 元组数组

描述 Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组. 语法 items()方法语法: dict.items() 参数 NA. 返回值 返回可遍历的(键, 值) 元组数组. 实例 以下实例展示了 items()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} print "Value : %s" % dict.items() 以上实例输出结果为: Value

python字典copy()方法

python 字典的copy()方法表面看就是深copy啊,明显独立 1 d = {'a':1, 'b':2} 2 c = d.copy() 3 print('d=%s c=%s' % (d, c)) Code1 结果: d={'a': 1, 'b': 2}  c={'a': 1, 'b': 2} 修改d,看看c变化没有. 1 d['a']=3 2 print('d=%s c=%s' % (d, c)) Code2 结果: d={'a': 3, 'b': 2}  c={'a': 3, 'b':