当前位置:天才代写 > tutorial > Python教程 > python操纵字典范例的常用要领总结

python操纵字典范例的常用要领总结

2017-11-03 08:00 星期五 所属: Python教程 浏览:1027

has_key()要领可以查抄字典中是否含有指定的键,假如有则返回True,不然就返回False。

语法名目:

dictionary_name.has_key(key)
dict1 = {'01':'yangry','02':'weild','03':'hexh','04':'huangmg'}
print dict1.has_key('02')
print dict1.has_key('08')
#result
True
False

2.clear()要领

用于排除字典中所有的项,无返回值。

利用方法:

dictionary_name.clear()

3,copy要领

copy()要领返回一个具有沟通键值对的新字典,名目如下:

dictionary_targetname = dictionary_sourcenme.copy()

4,fromkeys()要领

利用给定的键来成立新的字典,每个键值对应的值为None。

dictionary_name.fromkeys([key1,key2,…],(default_value))

5,update()要领

名目:

update_dictionary.update(source_dictionary)
mydict1 = {'1':'yy11','2':'yy22','3':'yy33'}
newdict = {'2':'success'}
mydict1.update(newdict)
print mydict1
#result
{'1': 'yy11', '3': 'yy33', '2': 'success'}

 

    关键字:

天才代写-代写联系方式