python - 元组的字典:为什么它不采用所有值?
问题描述
我将字典转换为元组,因此它可以是可散列的。
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'
我想这是因为元组中缺少数据。
解决方案
迭代一个 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))
>>>
推荐阅读
- c++ - 使用哈希映射计算总和为 0 的子数组的数量
- django - RuntimeWarning:DateTimeField(未绑定)收到一个天真的 datetime ,当增加持续时间直到 datetime.now
- .net - .Net 6 没有运行两个以上的 HostedServices
- c# - 找不到与此错误代码关联的文本。不能再分配 Internet 句柄
- android - 清单合并失败,未指定新值
- javascript - 此关键字不返回 html 元素
- reactjs - GET js chunk 404'd on all but one chunk
- c# - 启动任务,不要等待,仍然获取实例
- c# - 使用未排序整数的随机列表创建文本文件
- c - 使用单链表的程序在 C 中查找 char* 的子字符串时出现段错误