user2236794
user2236794

Reputation: 561

what data structure can I use

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

Upvotes: 0

Views: 62

Answers (2)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52213

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}}

Upvotes: 1

drali
drali

Reputation: 317

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

Upvotes: 0

Related Questions