Python3 字典 fromkeys() 方法
Python 3 教程
· 2019-02-06 15:27:57
描述
Python 字典 fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。
语法
fromkeys() 方法语法:
dict.fromkeys(seq[, value])
参数
- seq -- 字典键值列表。
- value -- 可选参数, 设置键序列(seq)对应的值,默认为 None。
返回值
该方法返回一个新字典。
实例
以下实例展示了 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}
不指定值:
实例
#!/usr/bin/python3
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)
以上实例输出结果为:
['key1': None, 'key2': None, 'key3': None]
点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html
Twenty Lectures on Algorithmic Game Theory
Tim Roughgarden / Cambridge University Press / 2016-8-31 / USD 34.99
Computer science and economics have engaged in a lively interaction over the past fifteen years, resulting in the new field of algorithmic game theory. Many problems that are central to modern compute......一起来看看 《Twenty Lectures on Algorithmic Game Theory》 这本书的介绍吧!
XML、JSON 在线转换
在线XML、JSON转换工具
UNIX 时间戳转换
UNIX 时间戳转换