#Python 字典(Dictionary) fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。
seq = ['a', 1, None]
dict1 = dict.fromkeys(seq, value)
dict2 = dict.fromkeys(seq, 11)
# or
# dict1 = {}.fromkeys(seq, value)
# dict2 = dict.fromkeys(seq, 11)
print(dict1) # {'a': None, 1: None, None: None}
print(dict2) # {'a': 11, 1: 11, None: 11}
dict3 = {}.fromkeys(dict2, 'abc')
print(dict3) # {'a': 'abc', 1: 'abc', None: 'abc'}
python fromkeys类方法
最新推荐文章于 2021-03-01 19:04:44 发布