python 示例
字典fromkeys()方法 (Dictionary fromkeys() Method)
fromkeys() method is used to create a dictionary with given keys and value.
fromkeys()方法用于创建具有给定键和值的字典。
Syntax:
句法:
dictionary_name.fromkeys(keys, value)
Parameter(s):
参数:
keys – It represents a container (iterable) with keys.
keys –它代表带有密钥的容器(可迭代)。
value – It is an optional parameter, that is used to specify the value. The default value of None.
value –这是一个可选参数,用于指定值。 默认值为None 。
Return value:
返回值:
The return type of this method is <class 'dict'>, it returns the newly created dictionary with specified keys and values.
此方法的返回类型为<class'dict'> ,它返回带有指定键和值的新创建的字典。
Example:
例:
# Python Dictionary fromkeys() Method with Example
# keys iterbale
keys = ('id', 'age', 'perc')
# value
value = 0
# creating dictionary with keys and value
x = dict.fromkeys(keys, value)
# printing dictionary
print("data of x dictionary...")
print(x)
# creating dictionary with keys only
y = dict.fromkeys(keys)
# printing dictionary
print("data of y dictionary...")
print(y)
Output
输出量
data of x dictionary...
{'perc': 0, 'id': 0, 'age': 0}
data of y dictionary...
{'perc': None, 'id': None, 'age': None}
翻译自: https://www.includehelp.com/python/dictionary-fromkeys-method-with-example.aspx
python 示例