访问python中包含列表的信息

时间:2012-04-20 12:45:18

标签: python

我正在编写一个脚本,可以帮助我记录我们的网络房间。

该脚本背后的想法是房间是一个列表,其中包含多个机架列表。机架列表包含名为module的列表,其中包含服务器/交换机/等。在模块列表中是带有电缆号码的实际端口。

例如:

[04/02, [MM02, [1, #1992, 2, #1993, 3, #1567 ....], MM03, [1, #1234 .....]], 04/03, [MM01, .........]]]

04/02 = First Rack

MM02 =该机架中的第一个模块

1 =端口号

#1992 =电缆号码

我希望你明白这一点。

我写的脚本比较房间列表中的电缆号码,看看是否有重复。现在它变得棘手:它应该用另一个端口的机架和模块替换电缆号码。这应该很简单,因为模块和机架是包含端口的那些列表的第一个元素,但我不知道如何访问信息。 (我是编程中的菜鸟)

2 个答案:

答案 0 :(得分:1)

如上所述,这里使用的更好的数据结构是嵌套dicts

data = {
    "04/02": {
        "MM02": {1: "#1992", 2: "#1993", 3: "#1567", ...},
        "MM03": {1: "#1234", ...},
        ... 
    },
    "04/03": {
        "MM01": ...
        ...
    },
    ...
}

然后你只需要data["04/02"]["MM02"] = {1: "#1992", 2: "#1993", 3: "#1567", ...}替换值,但是,这有一个缺点,即你需要手动创建子词典 - 但是,solutions to this problem例如:

from functools import partial
from collections import defaultdict

tripledict = partial(defaultdict, partial(defaultdict, dict))
mydict = tripledict()
mydict['foo']['bar']['foobar'] = 25

这些不仅具有可读性和可用性方面的优势,而且还具有访问速度。

答案 1 :(得分:0)

这是您正在寻找的Python类。它非常简单,所以如果你是菜鸟你说你是,你想学习:阅读并理解代码。
底部给出一些示例行来显示功能。对于多个机架,只需创建Rack()列表即可。祝你好运。

class Rack():
    def __init__(self, name):
        self.name = name
        self.modules = dict() 

    # port_cable_list should be in the form:
    # [(1, #1992), (2, #1993), (3, #1567)] 
    def add_module(self, name, port_cable_list):
        self.modules[name] = dict()
        for port, cable in port_cable_list:
            self.modules[name][port] = cable

    def remove_module(self, name):
        if name in self.modules:
            del self.modules[name]

    def add_port(self, module_name, port, cable):
        if module_name not in self.modules:
            self.modules[module_name][port] = cable
            return True
        return False

    def remove_port(self, module_name, port):
        if module_name in self.modules:
            if port in self.modules[module_name]:
                del self.modules[module_name][port]
                return True
            else:
                return False
        return False

    def module_exists(self, module_name):
        return module_name in self.modules

    def port_exists_in_module(self, module_name, port):
        if self.modules[module_name]:
            return port in self.modules[module_name]
        return False

    def print_module(self, module_name):
        if self.module_exists(module_name):
            print "%s\nPort\tCable" % (module_name)
            for port, cable in self.modules[module_name].items():
                print port, "\t", cable
            print
            return True
        return False

    def print_rack(self):
        print self.name + ':'
        for module_name in self.modules.keys():
            self.print_module(module_name)

SomeRack = Rack('04/02')
SomeRack.add_module("MM02", [(1, '#1992'), (2, '#1993'), (3, '#1567')])
SomeRack.add_module("MM03", [(1, '#1234')])
SomeRack.print_module("MM03")
SomeRack.print_rack()
SomeRack.remove_module("MM03")
print SomeRack.module_exists("MM03")
SomeRack.print_rack()
相关问题