Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:45:26+00:00 2026-06-09T18:45:26+00:00

This is kind of convoluted, so if I’m missing out on an easy construct

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:45:27+00:00Added an answer on June 9, 2026 at 6:45 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you know what this kind of problem is called, let me know (unless
Sorry to ask this kind of questions. But I am eager to know some
this kind of emergency, so please, can someone help me... I'm using movingboxes plugin
I know this kind of questions have been asked already many times before. The
I want this kind of an out put ID Status 100 Viewed 103 Not
Well this kind of n00b question but I still can't figure it out. I
this may sound kind of convoluted but basically i want to have a form
I know questions of this kind have been asked before, but my situation differs
I have this kind of code in some applications (from microsoft) [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Naming,
I have this kind of message when I start gdb : $ gdb a.out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.