0

This is kind of convoluted, so if I'm missing out on an easy construct for this, please let me know :)

I'm analysing the results of some matching experiments. At the end game, I want to be able to query things such as experiments[0]["cat"]["cat"], which yields the number of times "cat" was matched against "cat". Conversely, I could do experiments[0]["cat"]["dog"], when the first query was a cat and the match attempt was a dog.

The following is my code to populate this structure:

    # initializing the first layer, a list of dictionaries.
    experiments = []
    for assignment in assignments:
        match_sums = {}
        experiments.append(match_sums)


for i in xrange(len(classes)):
        for experiment in xrange(len(experiments)):
            # experiments[experiment][classes[i]] should hold a dictionary,
            # where the keys are the things that were matched against classes[i], 
            # and the value is the number of times this occurred.
            experiments[experiment][classes[i]] = collections.defaultdict(dict)

            # matches[experiment][i] is an integer for what the i'th match was in an experiment.
            # classes[j] for some integer j is the string name of the i'th match. could be "dog" or "cat".
            experiments[experiment][classes[i]][classes[matches[experiment][i]]] += 1
            total_class_sums[classes[i]] = total_class_sums.get(classes[i], 0) + 1

    print experiments[0]["cat"]["cat"]
    exit()

So clearly this is a bit convoluted. And I'm getting a value of "1" for the last match, rather than a full dictionary at experiments[0]["cat"]. Have I approached this wrong? What could the bug here be? Sorry for the craziness and thanks for any possible help!

8
  • You MUST use dictionaries? Wouldn't it be easier to go OOP on this one?
    – favoretti
    Aug 9, 2012 at 20:15
  • @favoretti perhaps... Though it's not immediately clear what kind of objects would simplify this. I can't change the original data format (the classes list or the matches list)
    – Jim
    Aug 9, 2012 at 20:20
  • You're code looks like it should work correctly -- you only define the first dictionary as a defaultdict so getting '1' from experiments[0]["cat"] seems strange... if you drop the specific experiment assignments do you still get a value of '1' from experiments[0]["cat"]?
    – Pyrce
    Aug 9, 2012 at 20:22
  • Please fix identation. It't not clear wheither print and exit are inside first loop or not
    – Odomontois
    Aug 9, 2012 at 20:29
  • Also it's strange. Why you should use dict of dicts? In the code you are creating defaultdict and always putting single value in it
    – Odomontois
    Aug 9, 2012 at 20:31

2 Answers 2

2

Two points:

  • Dictionary keys can be tuples; and
  • If you're counting things, use collections.Counter. (You can use defaultdict(int), but Counter is more useful.)

So, instead of

experiments[experiment][classes[i]][classes[matches[experiment][i]]] += 1

write

experiments = Counter()
...
experiments[experiment, classes[i], classes[matches[experiment][i]]] += 1
1
  • It's hard to tell from the question, but I have a feeling it should end up saying something even simpler, like experiments[experiment_index, cls, value] += 1, in the end. Aug 9, 2012 at 21:04
0

I just trying to guess your needs, so i tried to change order of your dimensions.

for className, classIdx in enumerate(classes):
    experiment = collections.defaultdict(list)
    experiments[className] = experiment
    for assignment,assignmentIdx in enumerate(assignments):
        counterpart = classes[matches[assignmentIdx][classIdx]]
        experiment[counterpart].append((assignment,assignmentIdx))

print(len(experiments["cat"]["cat"]), len(experiments["cat"]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.