0

I have a dump of system usage in a raw txt file. I would like to get it in this format. location, server, usage.

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.

Ultimately I would like to print all servers in location X

So will have for ex:

location1
  server1
     usage X
location1
   server2
      usage X
location2
    server1
      usage x
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
user2236794
  • 441
  • 3
  • 7
  • 22
  • 3
    _"Unless there is a way to store multiple elements with the same key..."_ `defaultdict(list)`. – Kevin Sep 18 '15 at 17:58
  • 2
    It would help if you specified the data format, for example, posted a line from the file. – drali Sep 18 '15 at 17:59
  • 1
    You should take a look at this question: Python creating a dictionary of lists - http://stackoverflow.com/questions/960733/python-creating-a-dictionary-of-lists – yasouser Sep 18 '15 at 18:00

2 Answers2

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}}
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
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
drali
  • 307
  • 1
  • 12