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

  • Home
  • SEARCH
  • 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 8218839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:56:19+00:00 2026-06-07T12:56:19+00:00

I want to create a dictionary that stores the name of a mathematical function,

  • 0

I want to create a dictionary that stores the name of a mathematical function, the input (a single number) and the result.

The structure should be something like cache = {procedure: {input:result}}

i.e.

cache = {'factorial': {4:24, 10:3628800, 20:2432902008176640000},
         'square': {2:4, 5:25, 10:100}
         'sum': {3:6, 5:10, 7:14}}

def factorial(n):
    #print "Running factorial"
    result = 1
    for i in range(2, n + 1):
        result = result * i
    return result

def square(n):
    result = n ^ 2
    return result

def sum(n):
    result = n * 2
    return result

I don’t want the cache dictionary to be pre-created in the sense that it may not know which mathematical functions it will store values for. I want to then create a function, named cached_execution, that will first check the cache if the function has been called for the input and if so return what is stored as the value for the input:result pair.

If not then compute the operation, store it in cache and return the value. If the function exists in the cache then another key/value pair should be created under it. If not then create a new entry for the function name and store the key/value of the input/result under it.

The structure of cached_execution is simple but what I can’t figure out is how to append to a dictionary. It seems append is not a method that is allowed for a dictionary structure. I have tried various things without success.

Appreciate the 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-07T12:56:20+00:00Added an answer on June 7, 2026 at 12:56 pm

    You might also want to look at Memoize. There are a number of possible implementations, 3 of which are on the Python.org wiki. Even if you write your own version, it is helpful to see how others have attacked the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to create a dictionary that maps digits to numbers less than
I want to create a custom dictionary that does not copy its keys (just
I have a dictionary that I read from a plist. I want to create
I want to create the dictionary of all the ViewModels. public static Dictionary<string, WeakReference>
I want to create a Python dictionary which holds values in a multidimensional array
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
I want to create a decorator that works like a property, only it calls
I want to create a Dictionary<DateTime, Dictionary<APerson, AVisit>> struct and I want to supply
I want to create a method that allows me to change arbitrary properties of

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.