我在原始txt文件中转储了系统用法。我想以这种格式得到它。 位置,服务器,用途。
我正在查找字典,但我有多个位置,这将是关键。除非有一种方法来存储具有相同键的多个元素,否则我不会看到字典如何工作。我将在python中执行此操作。我可以使用什么结构来获得这种格式的结果。
最终我想打印位置X的所有服务器
对于前者也是如此:
location1
server1
usage X
location1
server2
usage X
location2
server1
usage x
答案 0 :(得分:1)
您仍然可以使用字典,其中位置是键,服务器是包含用法的值。
>>> locations = collections.defaultdict(dict)
>>> locations['location1']['server1'] = 10000156567
>>> locations['location1']['server2'] = 10000453453
>>> locations['location2']['server1'] = 10000866646
{'location2': {'server1': 10000866646}, 'location1': {'server1': 10000156567, 'server2': 10000453453}}
答案 1 :(得分:0)
这个基本结构应该适合你:
# list of tuples to store the data
list = [("l1", "s1", "u1"), ("l1", "s2", "u2"), ("l2", "s3", "u1"), ("l2", "s4", "u2"), ("l3", "s5", "u2")]
# find server by location
result = []
for location, server, usage in list:
if location == "l2":
result.append((location, server, usage))
print result
# find server by usage
result = []
for location, server, usage in list:
if usage == "u2":
result.append((location, server, usage))
print result