首页 > 解决方案 > 元组的字典:为什么它不采用所有值?

问题描述

我将字典转换为元组,因此它可以是可散列的。

DATA_ILLUMINANTS = {
'LED': tuple({
    380.0: 0.006,
    385.0: 0.013,
    ...
    780.0: 0.108,})
    }

当我打印元组时,没有第二列数据,它是:

(380.0, 385.0, 390.0, 395.0, 400.0, ... , 780.0)

知道为什么吗?

我在另一个代码中使用“LED”元组返回以下错误:AttributeError: 'tuple' object has no attribute 'shape'我想这是因为元组中缺少数据。

标签: pythontupleshashable

解决方案


迭代一个 dict (这就是例如tuple()所做的)迭代键。

你会想要tuple({...}.items())一个 2 元组的元组。

>>> x = {1: 2, 3: 4, 5: 6}
>>> tuple(x)
(1, 3, 5)
>>> tuple(x.items())
((1, 2), (3, 4), (5, 6))
>>>

推荐阅读