Python字典 fromkeys() 方法
Python字典 fromkeys()函数返回具有键映射和特定值的字典。它从给定序列中创建一个具有特定值的新字典。
Syntax : fromkeys(seq, val)
Parameters :
- seq : The sequence to be transformed into a dictionary.
- val : Initial values that need to be assigned to the generated keys. Defaults to None.
Returns : A dictionary with keys mapped to None if no value is provided, else to the value provided in the field.
Python字典 fromkeys() 方法示例
示例 1:演示 fromkeys() 的工作原理
Python3
# Python 3 code to demonstrate
# working of fromkeys()
# initializing sequence
seq = {'a', 'b', 'c', 'd', 'e'}
# using fromkeys() to convert sequence to dict
# initializing with None
res_dict = dict.fromkeys(seq)
# Printing created dict
print("The newly created dict with None values : " + str(res_dict))
# using fromkeys() to convert sequence to dict
# initializing with 1
res_dict2 = dict.fromkeys(seq, 1)
# Printing created dict
print("The newly created dict with 1 as value : " + str(res_dict2))
Python3
# Python 3 code to demonstrate
# behaviour with mutable objects
# initializing sequence and list
seq = {'a', 'b', 'c', 'd', 'e'}
lis1 = [2, 3]
# using fromkeys() to convert sequence to dict
# using conventional method
res_dict = dict.fromkeys(seq, lis1)
# Printing created dict
print("The newly created dict with list values : "
+ str(res_dict))
# appending to lis1
lis1.append(4)
# Printing dict after appending
# Notice that append takes place in all values
print("The dict with list values after appending : "
+ str(res_dict))
lis1 = [2, 3]
print('\n')
# using fromkeys() to convert sequence to dict
# using dict. comprehension
res_dict2 = {key: list(lis1) for key in seq}
# Printing created dict
print("The newly created dict with list values : "
+ str(res_dict2))
# appending to lis1
lis1.append(4)
# Printing dict after appending
# Notice that append doesnt take place now.
print("The dict with list values after appending (no change) : "
+ str(res_dict2))
Python3
x = ('key1', 'key2', 'key3')
y = 0
d = dict.fromkeys(x, y)
print(d)
Python3
# Python3 code to demonstrate
# to initialize dictionary with list
# using fromkeys()
# using fromkeys() to construct
new_dict = dict.fromkeys(range(4), [])
# printing result
print ("New dictionary with empty lists as keys : " + str(new_dict))
输出 :
The newly created dict with None values : {‘d’: None, ‘a’: None, ‘b’: None, ‘c’: None, ‘e’: None}
The newly created dict with 1 as value : {‘d’: 1, ‘a’: 1, ‘b’: 1, ‘c’: 1, ‘e’: 1}
以可变对象为值的 fromdict() 的行为
fromdict() 也可以提供可变对象作为默认值。但是在这种情况下,对字典进行了深拷贝,即如果我们在原始列表中追加值,则追加发生在键的所有值中。
预防:某些字典理解技术可用于创建一个新列表作为键值,它不指向原始列表作为键值。
示例 2:使用可变对象演示行为
Python3
# Python 3 code to demonstrate
# behaviour with mutable objects
# initializing sequence and list
seq = {'a', 'b', 'c', 'd', 'e'}
lis1 = [2, 3]
# using fromkeys() to convert sequence to dict
# using conventional method
res_dict = dict.fromkeys(seq, lis1)
# Printing created dict
print("The newly created dict with list values : "
+ str(res_dict))
# appending to lis1
lis1.append(4)
# Printing dict after appending
# Notice that append takes place in all values
print("The dict with list values after appending : "
+ str(res_dict))
lis1 = [2, 3]
print('\n')
# using fromkeys() to convert sequence to dict
# using dict. comprehension
res_dict2 = {key: list(lis1) for key in seq}
# Printing created dict
print("The newly created dict with list values : "
+ str(res_dict2))
# appending to lis1
lis1.append(4)
# Printing dict after appending
# Notice that append doesnt take place now.
print("The dict with list values after appending (no change) : "
+ str(res_dict2))
输出:
The newly created dict with list values : {‘d’: [2, 3], ‘e’: [2, 3], ‘c’: [2, 3], ‘a’: [2, 3], ‘b’: [2, 3]}
The dict with list values after appending : {‘d’: [2, 3, 4], ‘e’: [2, 3, 4], ‘c’: [2, 3, 4], ‘a’: [2, 3, 4], ‘b’: [2, 3, 4]}
The newly created dict with list values : {‘d’: [2, 3], ‘e’: [2, 3], ‘c’: [2, 3], ‘a’: [2, 3], ‘b’: [2, 3]}
The dict with list values after appending (no change) : {‘d’: [2, 3], ‘e’: [2, 3], ‘c’: [2, 3], ‘a’: [2, 3], ‘b’: [2, 3]}
示例 3: Python字典 fromkeys() 默认值
Python3
x = ('key1', 'key2', 'key3')
y = 0
d = dict.fromkeys(x, y)
print(d)
输出:
{'key1': 0, 'key2': 0, 'key3': 0}
示例 4:带有空列表的Python字典 fromkeys()
Python3
# Python3 code to demonstrate
# to initialize dictionary with list
# using fromkeys()
# using fromkeys() to construct
new_dict = dict.fromkeys(range(4), [])
# printing result
print ("New dictionary with empty lists as keys : " + str(new_dict))
输出:
New dictionary with empty lists as keys : {0: [], 1: [], 2: [], 3: []}