views:

90

answers:

3

I have several hundred thousand endpoint URLs that I want to generate stats for. For example I have:

/a/b/c
/a/b/d
/a/c/d
/b/c/d
/b/d/e
/a/b/c
/b/c/d

I want to create a dictionary that looks like this

{
   {'a':
        {'b':
             {'c': 2 },
             {'d': 1 }
        },
        {'c':
             {'d': 1 }
        }
    },
    {'b':
        {'c':
             {'d': 2}
        },
        {'d':
             {'e': 1}
        }
    }
}

Any clever ways to do this?

EDIT

I should mention that the paths are not always 3 parts. There might be /a/b/c/d/e/f/g/h... etc, etc.

+2  A: 

If the paths all look like in your example, this would work:

counts = {}
for p in paths:
   parts = p.split('/')
   branch = counts
   for part in parts[1:-1]:
      branch = branch.setdefault(part, {})
   branch[parts[-1]] = 1 + branch.get(parts[-1], 0)

This uses dictionary methods like setdefault() and get() to avoid having to write a lot of if-statements.

Note that this will not work if a path that has subdirectories can also appear on it's own. Then it's not clear if the corresponding part of counts should contain a number or another dictionary. In this case it would probably be best to store both a count and a dict for each node, using a tuple or a custom class.

The basic algorithm stays the same:

class Stats(object):
   def __init__(self):
      self.count = 0
      self.subdirs = {}

counts = Stats()
for p in paths:
   parts = p.split('/')
   branch = counts
   for part in parts[1:]:
      branch = branch.subdirs.setdefault(part, Stats())
   branch.count += 1

With some pretty printing you get:

def printstats(stats, indent=''):
   print indent + str(stats.count) + ' times'
   for (d, s) in stats.subdirs.items():
       print indent + d + ':'
       printstats(s, indent + '  ')

>>> printstats(counts)
0 times
a:
  0 times
  c:
    0 times
    d:
      1 times
  b:
    0 times
    c:
      2 times
    d:
      1 times
...
sth
This is somewhat similar to what I was already doing. Though I think recursion will work better because of the unknown number of items in the p.split('/')
sberry2A
In your example all items have the same number of parts, so I though I would skip the additional complexity...
sth
This will allow a variable number of parts - although not within the same set of paths (i.e. it could handle `['/a/b', '/b/c', '/a/c']` or `['/a/b/c/d', '/d/c/b/a']`, but not `['/a/b', '/a/b/c', '/a/b/c/d']`)
Smashery
@Smashery: It handles all of that now...
sth
Very nice solution.
Smashery
A: 

EDIT:

I've amended my code to fit your last comment, above (no complex data structure now).

def dictizeString(string, dictionary):
    while string.startswith('/'):
        string = string[1:]
    parts = string.split('/', 1)
    if len(parts) > 1:
        branch = dictionary.setdefault(parts[0], {})
        dictizeString(parts[1], branch)
    else:
        if dictionary.has_key(parts[0]):
             # If there's an addition error here, it's because invalid data was added
             dictionary[parts[0]] += 1
        else:
             dictionary[parts[0]] = 1

It will store a list of [frequency, dictionary] for each item.

Test case

>>> d = {}
>>> dictizeString('/a/b/c/d', d)
>>> dictizeString('/a/b/c/d', d)
>>> dictizeString('/a/b/c/d', d)
>>> dictizeString('/a/b/c/d', d)
>>> dictizeString('/a/b/e', d)
>>> dictizeString('/c', d)
>>> d
{'a': {'b': {'c': {'d': 4}, 'e': 1}}, 'c': 1}
Smashery
This doesn't seem right. Assuming I understand this correctly, then each time you add a path, you set the count for that URL to 1 (rather than incrementing the count), and destroy any counts for longer paths with the same prefix.
Michael Williamson
Yeah, I made that mistake in the first version of my code. I've since edited to fix that. Thanks! Seems to work fine now.
Smashery
A: 

Here's my attempt:

class Result(object):
    def __init__(self):
        self.count = 0
        self._sub_results = {}

    def __getitem__(self, key):
        if key not in self._sub_results:
            self._sub_results[key] = Result()
        return self._sub_results[key]

    def __str__(self):
        return "(%s, %s)" % (self.count, self._sub_results)

    def __repr__(self):
        return str(self)

def process_paths(paths):
    path_result = Result()
    for path in paths:
        components = path[1:].split("/")
        local_result = path_result
        for component in components:
            local_result = local_result[component]
        local_result.count += 1
    return path_result

I've wrapped up some of the logic into the Result class to try and make the algorithm itself a little clearer.

Michael Williamson