[英]Dictionary give the values to the certain key, but the others get the same values
I want to create a dictionary to record the accurancy when I run the program.There are some problem when I give the values to the certain key我想创建一个字典来记录我运行程序时的准确度。当我给某个键赋值时出现了一些问题
Here, I create a dictionary to record the accurancy when it trains or validate在这里,我创建了一个字典来记录训练或验证时的准确度
acc = dict.fromkeys(['train', 'val'], dict.fromkeys(['pa', 'iou', 'dice'], 0))
The original dictionary looks like this原来的字典是这样的
{'train': {'pa': 0, 'iou': 0, 'dice': 0}, 'val': {'pa': 0, 'iou': 0, 'dice': 0}}
Now, I give the some values for testing现在,我给出了一些测试值
acc['train']['pa'] = 0.9
acc['train']['iou'] = 0.8
acc['train']['dice'] = 0.7
The problems is all the values in key val
becomes the same该问题是关键中的所有值
val
变得相同
{'train': {'pa': 0.9, 'iou': 0.8, 'dice': 0.7}, 'val': {'pa': 0.9, 'iou': 0.8, 'dice': 0.7}}
If the provided value is a mutable object (whose value can be modified) like list, dictionary, etc., when the mutable object is modified, each element of the sequence also gets updated.如果提供的值是一个可变对象(其值可以修改),如列表、字典等,当可变对象被修改时,序列的每个元素也会更新。 This is because, each element is assigned a reference to the same object (points to the same object in the memory).
这是因为,每个元素被分配给相同的对象的引用(指向在存储器中的相同的对象)。
In your problem, dict.fromkeys(['pa', 'iou', 'dice'], 0)
returns a dictionary which is mutable type, so each element is assigned to the same object.在您的问题中,
dict.fromkeys(['pa', 'iou', 'dice'], 0)
返回一个可变类型的字典,因此每个元素都分配给同一个对象。
To avoid this issue, you can use dictionary comprehension.为了避免这个问题,你可以使用字典理解。
from copy import copy
# keys
keys = ['train', 'val']
value = dict.fromkeys(['pa', 'iou', 'dice'], 0 )
acc = { key : copy(value) for key in keys }
print(acc)
# updating the value
acc['train']['pa'] = 2
print(acc)
Source: https://www.programiz.com/python-programming/methods/dictionary/fromkeys来源: https://www.programiz.com/python-programming/methods/dictionary/fromkeys
That's because dict.fromkeys(['pa', 'iou', 'dice'], 0)
is called exactly once, not once for each key in acc
.那是因为
dict.fromkeys(['pa', 'iou', 'dice'], 0)
被调用一次,而不是对acc
每个键调用一次。
>>> acc['train'] is acc['val']
True
The dictionaries are the same object in memory, you just have two ways to access it - via acc['train']
and acc['val']
.字典是内存中的同一个对象,您只有两种访问方式 - 通过
acc['train']
和acc['val']
。
You could use a defaultdict
with a function that creates a new dictionary on demand:您可以将
defaultdict
与按需创建新字典的函数一起使用:
>>> from collections import defaultdict
>>> d = defaultdict(lambda: {'pa': 0, 'iou': 0, 'dice': 0})
>>> d['train']
{'pa': 0, 'iou': 0, 'dice': 0}
>>> d['val']
{'pa': 0, 'iou': 0, 'dice': 0}
>>> d['train']['pa'] = 1
>>> d['train']
{'pa': 1, 'iou': 0, 'dice': 0}
>>> d['val']
{'pa': 0, 'iou': 0, 'dice': 0}
However, be aware that this creates a new dictionary each time you access a new key:但是,请注意,每次访问新键时,这都会创建一个新字典:
>>> d['foo']
{'pa': 0, 'iou': 0, 'dice': 0}
If you don't want that, create a dictionary for each key manually with a dict comprehension:如果您不希望那样,请使用 dict 理解为每个键手动创建一个字典:
>>> keys = ['train', 'val']
>>> d = {k:{'pa': 0, 'iou': 0, 'dice': 0} for k in keys}
>>> d['train']['pa'] = 1
>>> d['train']
{'pa': 1, 'iou': 0, 'dice': 0}
>>> d['val']
{'pa': 0, 'iou': 0, 'dice': 0}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.