相关疑难解决方法(0)

为什么dict.get(key)而不是dict [key]?

今天,我遇到了这个dict方法get,它给出了字典中的一个键,返回了相关的值.

这个功能用于什么目的?如果我想找到与字典中的键相关联的值,我可以这样做dict[key],它返回相同的东西:

dictionary = {"Name": "Harry", "Age": 17}
dictionary["Name"]
dictionary.get("Name")
Run Code Online (Sandbox Code Playgroud)

python dictionary

553
推荐指数
11
解决办法
46万
查看次数

dict.fromkeys都指向同一个列表

这让我有点悲伤......

我从列表中创建了一个字典

l = ['a','b','c']
d = dict.fromkeys(l, [0,0]) # initializing dictionary with [0,0] as values

d['a'] is d['b'] # returns True
Run Code Online (Sandbox Code Playgroud)

如何使字典的每个值成为单独的列表?这是否可能不迭代所有键并将它们设置为等于列表?我想修改一个列表而不更改所有其他列表.

python

55
推荐指数
3
解决办法
1万
查看次数

标签 统计

python ×2

dictionary ×1