Python数据类型,是什么样的数据结构?

2024-09-30 18:21:53 发布

您现在位置:Python中文网/ 问答频道 /正文

reshape, crop = {
1: ((1952, 3264), (1944, 3240)),
2: ((2480, 4128), (2464, 4100)),
}

offset = {1: 6404096, 2: 10270208,}[ver]

#where ver is defined as a dictionary

ver = {
    'RP_ov5647': 1,
    'RP_imx219': 2,
    }[camera.exif_tags['IFD0.Model']]

这些是什么样的结构?ver报告为int类型。我很困惑。 在offset的作业中[ver]是如何工作的?你知道吗


Tags: cropdictionaryisastagswhererpoffset
2条回答

让我们看看这里。。。你知道吗

reshape, crop = {
1: ((1952, 3264), (1944, 3240)),
2: ((2480, 4128), (2464, 4100)),
}

这里是用键12定义字典。每个键都有一个元组。我相信真正的代码中有更多的条目,在最后一项上用逗号表示。然而,这个对象的大部分会丢失到垃圾收集中。对reshapecrop的元组赋值将导致只存储密钥。所以这个结果是一样的:

reshape = 1
crop = 2

没用就有趣。下一个。。。你知道吗

offset = {1: 6404096, 2: 10270208,}[ver]

所以这里定义了另一个字典,其中包含键12以及与它们相关联的长整数值。然后它用ver索引这个字典,并将这个索引处的值赋给offset。因为ver还没有定义,这实际上会导致一个异常。假设在前面的代码之前给出了以下代码:

#where ver is defined as a dictionary

ver = {
    'RP_ov5647': 1,
    'RP_imx219': 2,
    }[camera.exif_tags['IFD0.Model']]

我们还有一本字典。这一次键是'RP_ov5647''RP_imx219',值是12。此词典的索引值为camera.exif_tags['IFD0.Model']。假设camera.exif_tags是具有键控索引的对象,其中一个索引是'IFD0.Model',结果值是'RP_ov5647''RP_imx219',则ver将被分配12。你知道吗

因此,1或2将用于索引我们的上述偏移量值,从而导致偏移量被指定为640409610270208

简而言之,这都是一堆语法上有效的东西。谢谢分享。你知道吗

首先,您的初始语句不会做(我认为)您想做的事情:赋值左边有两个变量,右边只有一个值一个字典。我想你最后错过了[ver]。如前所述,该语句将构建字典,提取键(1,2),并将这些键赋给变量,而忽略维度元组。你知道吗

上面的变量ver是在字典中查找的整数。你知道吗

这段代码似乎是从模型名开始设计的,并根据该模型查找适当的参数。让我们按正确的顺序看代码,并使用几个中间变量。你知道吗

# Dictionary to look up version number (1 or 2),
#   given the model name.
model_to_version = {
    'RP_ov5647': 1,
    'RP_imx219': 2,
    }

# Get the model name from the camera info object.
# Here on Stack Overflow, we have no idea of the structure,
#   but merely have to trust that this works.
model_name = camera.exif_tags['IFD0.Model']

# Now, get the version number we're using as an internal key.
# This will return 1 or 2.
version = model_to_version[model_name]

# The offset look-up is unchanged.
offset = {1: 6404096, 2: 10270208}[version]

# Dictionary to look up sizing information (shape & crop).
# Each entry is a pair of x-y dimension pairs.
size_info = {
    1: ((1952, 3264), (1944, 3240)),
    2: ((2480, 4128), (2464, 4100)),
}

# Finally, look up the proper set of sizes:
shaping, crop = size_info[ver]

在所有这些之后,我们得到了shapingcrop对于这个相机模型具有适当的尺寸,并且offset在输入流中具有适当的像素偏移(我假设)。你知道吗

这就是你需要的吗?你知道吗

相关问题 更多 >