python - 字典中的命名元组
问题描述
我正在尝试使用命名元组,如下所示:
from collections import namedtuple
thermometer = namedtuple('thermometer', 'name, max, min')
mac_dict = {
"1234" : thermometer("warehouse1", 15, 17),
"123B" : thermometer("warehouse2", 11, 19),
"124C" : thermometer("serverroom", 12, 34)
}
mac_address = "1234"
print mac_dict[mac_address].thermometer.max
但这以:
AttributeError: 'thermometer' object has no attribute 'thermometer'
我可以以某种方式修复它吗?
解决方案
正如错误所说
mac_dict[mac_address]
是温度计。
只需解决其最大值
mac_dict[mac_address].max
推荐阅读
- r - XML 安装似乎失败并且未在 R 上加载
- ruby-on-rails - 即使列为 nil Rspec 也通过验证
- android - 字符串中的自动完成功能未在 android studio (Kotlin) 上显示
- php - 使用 UUID 将权限分配给 spatie/laravel-permission 中的角色时的非法偏移类型
- pandas - 我在 k=f1_score(temp, train_y) 上收到“DataFrame object is not callable”
- c++ - C++ 队列函数调用
- javascript - 用正则表达式替换值
- python - 如何从列表(txt 文件)中读取字符串并将它们打印为整数、字符串和浮点数?
- css - Flexbox 动态布局
- python - 如何从 2 个字符串生成唯一键?