首页 > 解决方案 > 字典中的命名元组

问题描述

我正在尝试使用命名元组,如下所示:

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'

我可以以某种方式修复它吗?

标签: pythonpython-2.7

解决方案


正如错误所说

mac_dict[mac_address]

温度计。

只需解决其最大值

mac_dict[mac_address].max

推荐阅读