I have a dump of system usage in a raw txt file. I would like to get it in this format. location, server, usage.
我在原始txt文件中转储了系统用法。我想以这种格式得到它。位置,服务器,用法。
I was looking into a dictionary, but I have multiple locations which will be the key. Unless there is a way to store multiple elements with the same key, I dont see how a dictionary will work. I will be doing this in python. What structure can I use to get my result in this format.
我正在查看字典,但我有多个位置,这将是关键。除非有一种方法来存储具有相同键的多个元素,否则我不会看到字典如何工作。我将在python中执行此操作。我可以使用什么结构来获得此格式的结果。
Ultimately I would like to print all servers in location X
最后,我想打印位置X中的所有服务器
So will have for ex:
所以将有前者:
location1
server1
usage X
location1
server2
usage X
location2
server1
usage x
2 个解决方案
#1
1
You can still use dictionary where locations are keys and servers are values containing usages.
您仍然可以使用字典,其中位置是键,服务器是包含用法的值。
>>> locations = collections.defaultdict(dict)
>>> locations['location1']['server1'] = 10000156567
>>> locations['location1']['server2'] = 10000453453
>>> locations['location2']['server1'] = 10000866646
{'location2': {'server1': 10000866646}, 'location1': {'server1': 10000156567, 'server2': 10000453453}}
#2
0
This basic structure should work for you:
这个基本结构应该适合你:
# 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
#1
1
You can still use dictionary where locations are keys and servers are values containing usages.
您仍然可以使用字典,其中位置是键,服务器是包含用法的值。
>>> locations = collections.defaultdict(dict)
>>> locations['location1']['server1'] = 10000156567
>>> locations['location1']['server2'] = 10000453453
>>> locations['location2']['server1'] = 10000866646
{'location2': {'server1': 10000866646}, 'location1': {'server1': 10000156567, 'server2': 10000453453}}
#2
0
This basic structure should work for you:
这个基本结构应该适合你:
# 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