字典只是求解
dict1 ={}dict1.fromkeys(range(10),'赞')
为什么,dict1还是{}空字典 别偷懒,正常写不行吗 >>> dict1 = dict.fromkeys(range(10), '赞')
>>> dict1
{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞'} 冬雪雪冬 发表于 2020-6-10 21:50
先创建空字典,直接dict1.fromkeys()不行吗?
lingedu 发表于 2020-6-10 22:12
先创建空字典,直接dict1.fromkeys()不行吗?
与列表的append等操作不同,fromkeys不是修改原字典,而是创建新的字典。>>> dict1 ={}
>>> dict1 = dict1.fromkeys(range(10),'赞')
>>> dict1
{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞'}
页:
[1]