这篇教程Python 字典(Dictionary) fromkeys()方法写得很实用,希望能帮到您。 描述Python 字典 fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。 语法fromkeys()方法语法: dict.fromkeys(seq[, value]) 参数- seq -- 字典键值列表。
- value -- 可选参数, 设置键序列(seq)的值。
返回值该方法返回一个新字典。 实例以下实例展示了 fromkeys()函数的使用方法: 实例(Python 2.0+) seq = ('Google', 'Codercto', 'Taobao') dict = dict.fromkeys(seq)print "新字典为 : %s" % str(dict) dict = dict.fromkeys(seq, 10)print "新字典为 : %s" % str(dict) 以上实例输出结果为: 新字典为 : {'Google': None, 'Taobao': None, 'Codercto': None}新字典为 : {'Google': 10, 'Taobao': 10, 'Codercto': 10} Python 字典(Dictionary) copy()方法 Python 字典(Dictionary) get()方法 |