3 | radiansdict.fromkeys() 创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值 描述Python 字典 fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。 语法fromkeys()方法语法: dict.fromkeys(seq[, value])) 参数
返回值该方法返回列表。 实例以下实例展示了 fromkeys()函数的使用方法: #!/usr/bin/python3
seq = ('name', 'age', 'sex')
dict = dict.fromkeys(seq)
print ("新的字典为 : %s" % str(dict))
dict = dict.fromkeys(seq, 10)
print ("新的字典为 : %s" % str(dict)) 以上实例输出结果为: 新的字典为 : {'age': None, 'name': None, 'sex': None}
新的字典为 : {'age': 10, 'name': 10, 'sex': 10}
|
9 | radiansdict.update(dict2) 把字典dict2的键/值对更新到dict里 描述Python 字典 update() 函数把字典dict2的键/值对更新到dict里。 语法update()方法语法: dict.update(dict2) 参数
返回值该方法没有任何返回值。 实例以下实例展示了 update()函数的使用方法: #!/usr/bin/python3 dict = {'Name': 'Runoob', 'Age': 7} dict2 = {'Sex': 'female' } dict.update(dict2) print ("更新字典 dict : ", dict) 以上实例输出结果为: 更新字典 dict : {'Name': 'Runoob', 'Age': 7, 'Sex': 'female'} |