RE: Can you help me with this memoization simple example?
I am not sure if it was made clear that there is a general rule in python for what is HASHABLE and lists are changeable while tuples are not so the latter can be hashed as a simple copy of a list, albeit the contents must also be immutable. The memorize function uses a dictionary to store things and thus the things are hashed to decide how to store it in the inner representation of a dictionary and anything new that you want to look up in the dictionary has similar considerations as it is hashed to see where in the dictionary to look for it. Of course, if you add enough overhead and the memorize function you make gets relatively few requests that are identical, it may not be worthwhile. -Original Message- From: Python-list On Behalf Of MRAB via Python-list Sent: Sunday, March 31, 2024 3:24 PM To: python-list@python.org Subject: Re: Can you help me with this memoization simple example? On 2024-03-31 09:04, marc nicole wrote: > Thanks for the first comment which I incorporated > > but when you say "You can't use a list as a key, but you can use a > tuple as a key, > provided that the elements of the tuple are also immutable." > > does it mean the result of sum of the array is not convenient to use > as key as I do? > Which tuple I should use to refer to the underlying list value as you > suggest? > I was suggesting using `tuple` on the argument: def memoize(f): cache = {} def g(*args): key = tuple(args[0]), args[1] if key not in cache: cache[key] = f(args[0], args[1]) return cache[key] return g > Anything else is good in my code ? > > Thanks > > Le dim. 31 mars 2024 à 01:44, MRAB via Python-list > a écrit : > > On 2024-03-31 00:09, marc nicole via Python-list wrote: > > I am creating a memoization example with a function that adds up > / averages > > the elements of an array and compares it with the cached ones to > retrieve > > them in case they are already stored. > > > > In addition, I want to store only if the result of the function > differs > > considerably (passes a threshold e.g. 50 below). > > > > I created an example using a decorator to do so, the results > using the > > decorator is slightly faster than without the memoization which > is OK, but > > is the logic of the decorator correct ? anybody can tell me ? > > > > My code is attached below: > > > > > > > > import time > > > > > > def memoize(f): > > cache = {} > > > > def g(*args): > > if args[1] == "avg": > > sum_key_arr = sum(list(args[0])) / len(list(args[0])) > > 'list' will iterate over args[0] to make a list, and 'sum' will > iterate > over that list. > > It would be simpler to just let 'sum' iterate over args[0]. > > > elif args[1] == "sum": > > sum_key_arr = sum(list(args[0])) > > if sum_key_arr not in cache: > > for ( > > key, > > value, > > ) in ( > > cache.items() > > ): # key in dict cannot be an array so I use the > sum of the > > array as the key > > You can't use a list as a key, but you can use a tuple as a key, > provided that the elements of the tuple are also immutable. > > > if ( > > abs(sum_key_arr - key) <= 50 > > ): # threshold is great here so that all > values are > > approximated! > > # print('approximated') > > return cache[key] > > else: > > # print('not approximated') > > cache[sum_key_arr] = f(args[0], args[1]) > > return cache[sum_key_arr] > > > > return g > > > > > > @memoize > > def aggregate(dict_list_arr, operation): > > if operation == "avg": > > return sum(list(dict_list_arr)) / len(list(dict_list_arr)) > > if operation == "sum": > > return sum(list(dict_list_arr)) > > return None > > > > > > t = time.time() > > for i in range(200, 15000): > > res = aggregate(list(range(i)), "avg") > > > > elapsed = time.time() - t > > print(res) > > print(elapsed) > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Can you help me with this memoization simple example?
On 2024-03-31 09:04, marc nicole wrote: Thanks for the first comment which I incorporated but when you say "You can't use a list as a key, but you can use a tuple as a key, provided that the elements of the tuple are also immutable." does it mean the result of sum of the array is not convenient to use as key as I do? Which tuple I should use to refer to the underlying list value as you suggest? I was suggesting using `tuple` on the argument: def memoize(f): cache = {} def g(*args): key = tuple(args[0]), args[1] if key not in cache: cache[key] = f(args[0], args[1]) return cache[key] return g Anything else is good in my code ? Thanks Le dim. 31 mars 2024 à 01:44, MRAB via Python-list a écrit : On 2024-03-31 00:09, marc nicole via Python-list wrote: > I am creating a memoization example with a function that adds up / averages > the elements of an array and compares it with the cached ones to retrieve > them in case they are already stored. > > In addition, I want to store only if the result of the function differs > considerably (passes a threshold e.g. 50 below). > > I created an example using a decorator to do so, the results using the > decorator is slightly faster than without the memoization which is OK, but > is the logic of the decorator correct ? anybody can tell me ? > > My code is attached below: > > > > import time > > > def memoize(f): > cache = {} > > def g(*args): > if args[1] == "avg": > sum_key_arr = sum(list(args[0])) / len(list(args[0])) 'list' will iterate over args[0] to make a list, and 'sum' will iterate over that list. It would be simpler to just let 'sum' iterate over args[0]. > elif args[1] == "sum": > sum_key_arr = sum(list(args[0])) > if sum_key_arr not in cache: > for ( > key, > value, > ) in ( > cache.items() > ): # key in dict cannot be an array so I use the sum of the > array as the key You can't use a list as a key, but you can use a tuple as a key, provided that the elements of the tuple are also immutable. > if ( > abs(sum_key_arr - key) <= 50 > ): # threshold is great here so that all values are > approximated! > # print('approximated') > return cache[key] > else: > # print('not approximated') > cache[sum_key_arr] = f(args[0], args[1]) > return cache[sum_key_arr] > > return g > > > @memoize > def aggregate(dict_list_arr, operation): > if operation == "avg": > return sum(list(dict_list_arr)) / len(list(dict_list_arr)) > if operation == "sum": > return sum(list(dict_list_arr)) > return None > > > t = time.time() > for i in range(200, 15000): > res = aggregate(list(range(i)), "avg") > > elapsed = time.time() - t > print(res) > print(elapsed) -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Can you help me with this memoization simple example?
Thanks for the first comment which I incorporated but when you say "You can't use a list as a key, but you can use a tuple as a key, provided that the elements of the tuple are also immutable." does it mean the result of sum of the array is not convenient to use as key as I do? Which tuple I should use to refer to the underlying list value as you suggest? Anything else is good in my code ? Thanks Le dim. 31 mars 2024 à 01:44, MRAB via Python-list a écrit : > On 2024-03-31 00:09, marc nicole via Python-list wrote: > > I am creating a memoization example with a function that adds up / > averages > > the elements of an array and compares it with the cached ones to retrieve > > them in case they are already stored. > > > > In addition, I want to store only if the result of the function differs > > considerably (passes a threshold e.g. 50 below). > > > > I created an example using a decorator to do so, the results using the > > decorator is slightly faster than without the memoization which is OK, > but > > is the logic of the decorator correct ? anybody can tell me ? > > > > My code is attached below: > > > > > > > > import time > > > > > > def memoize(f): > > cache = {} > > > > def g(*args): > > if args[1] == "avg": > > sum_key_arr = sum(list(args[0])) / len(list(args[0])) > > 'list' will iterate over args[0] to make a list, and 'sum' will iterate > over that list. > > It would be simpler to just let 'sum' iterate over args[0]. > > > elif args[1] == "sum": > > sum_key_arr = sum(list(args[0])) > > if sum_key_arr not in cache: > > for ( > > key, > > value, > > ) in ( > > cache.items() > > ): # key in dict cannot be an array so I use the sum of the > > array as the key > > You can't use a list as a key, but you can use a tuple as a key, > provided that the elements of the tuple are also immutable. > > > if ( > > abs(sum_key_arr - key) <= 50 > > ): # threshold is great here so that all values are > > approximated! > > # print('approximated') > > return cache[key] > > else: > > # print('not approximated') > > cache[sum_key_arr] = f(args[0], args[1]) > > return cache[sum_key_arr] > > > > return g > > > > > > @memoize > > def aggregate(dict_list_arr, operation): > > if operation == "avg": > > return sum(list(dict_list_arr)) / len(list(dict_list_arr)) > > if operation == "sum": > > return sum(list(dict_list_arr)) > > return None > > > > > > t = time.time() > > for i in range(200, 15000): > > res = aggregate(list(range(i)), "avg") > > > > elapsed = time.time() - t > > print(res) > > print(elapsed) > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Can you help me with this memoization simple example?
On 2024-03-31 00:09, marc nicole via Python-list wrote: I am creating a memoization example with a function that adds up / averages the elements of an array and compares it with the cached ones to retrieve them in case they are already stored. In addition, I want to store only if the result of the function differs considerably (passes a threshold e.g. 50 below). I created an example using a decorator to do so, the results using the decorator is slightly faster than without the memoization which is OK, but is the logic of the decorator correct ? anybody can tell me ? My code is attached below: import time def memoize(f): cache = {} def g(*args): if args[1] == "avg": sum_key_arr = sum(list(args[0])) / len(list(args[0])) 'list' will iterate over args[0] to make a list, and 'sum' will iterate over that list. It would be simpler to just let 'sum' iterate over args[0]. elif args[1] == "sum": sum_key_arr = sum(list(args[0])) if sum_key_arr not in cache: for ( key, value, ) in ( cache.items() ): # key in dict cannot be an array so I use the sum of the array as the key You can't use a list as a key, but you can use a tuple as a key, provided that the elements of the tuple are also immutable. if ( abs(sum_key_arr - key) <= 50 ): # threshold is great here so that all values are approximated! # print('approximated') return cache[key] else: # print('not approximated') cache[sum_key_arr] = f(args[0], args[1]) return cache[sum_key_arr] return g @memoize def aggregate(dict_list_arr, operation): if operation == "avg": return sum(list(dict_list_arr)) / len(list(dict_list_arr)) if operation == "sum": return sum(list(dict_list_arr)) return None t = time.time() for i in range(200, 15000): res = aggregate(list(range(i)), "avg") elapsed = time.time() - t print(res) print(elapsed) -- https://mail.python.org/mailman/listinfo/python-list
Can you help me with this memoization simple example?
I am creating a memoization example with a function that adds up / averages the elements of an array and compares it with the cached ones to retrieve them in case they are already stored. In addition, I want to store only if the result of the function differs considerably (passes a threshold e.g. 50 below). I created an example using a decorator to do so, the results using the decorator is slightly faster than without the memoization which is OK, but is the logic of the decorator correct ? anybody can tell me ? My code is attached below: import time def memoize(f): cache = {} def g(*args): if args[1] == "avg": sum_key_arr = sum(list(args[0])) / len(list(args[0])) elif args[1] == "sum": sum_key_arr = sum(list(args[0])) if sum_key_arr not in cache: for ( key, value, ) in ( cache.items() ): # key in dict cannot be an array so I use the sum of the array as the key if ( abs(sum_key_arr - key) <= 50 ): # threshold is great here so that all values are approximated! # print('approximated') return cache[key] else: # print('not approximated') cache[sum_key_arr] = f(args[0], args[1]) return cache[sum_key_arr] return g @memoize def aggregate(dict_list_arr, operation): if operation == "avg": return sum(list(dict_list_arr)) / len(list(dict_list_arr)) if operation == "sum": return sum(list(dict_list_arr)) return None t = time.time() for i in range(200, 15000): res = aggregate(list(range(i)), "avg") elapsed = time.time() - t print(res) print(elapsed) -- https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: Can you help me with this Python question?
Well, although I never used pandas and never will, if that's about artworks, that's mine. Obviously, you need to iterate columns and sum values returned by the snippet you provided. A quick search tells us to use colums property. So, it might look like this: na_sum = sum(df[name].isnull().sum() for name in df.columns) Axy On 13/10/2022 13:44, Sarah Wallace wrote: For a python class I am taking.. In this challenge, you'll be working with a DataFrame that contains data about artworks, and it contains many missing values. Your task is to create a variable called na_sum that contains the total number of missing values in the DataFrame. When that's completed, print out your answer! Hint: The code given below will give you the number of missing (NaN) values for the *Name* column in the DataFrame. How would you edit the code to get the missing values for every column in the DataFrame? Extra hint: You'll be returning a single number which is the final sum() of everything. df['Name'].isnull().sum() -- https://mail.python.org/mailman/listinfo/python-list
Fwd: Can you help me with this Python question?
For a python class I am taking.. In this challenge, you'll be working with a DataFrame that contains data about artworks, and it contains many missing values. Your task is to create a variable called na_sum that contains the total number of missing values in the DataFrame. When that's completed, print out your answer! Hint: The code given below will give you the number of missing (NaN) values for the *Name* column in the DataFrame. How would you edit the code to get the missing values for every column in the DataFrame? Extra hint: You'll be returning a single number which is the final sum() of everything. df['Name'].isnull().sum() -- Thanks! *Sarah Wallace* sarah.wallac...@gmail.com -- Thanks! *Sarah Wallace* sarah.wallac...@gmail.com 214.300.1064 -- https://mail.python.org/mailman/listinfo/python-list
Re: Can you help me solve this?
Pieter van Oostrum writes: > Joseph Nail writes: > >> Hello, >> I have one problem. Somehow in my function when I wrote y=x, they are the >> same variable and then it also changes age or height (which were x) in the >> main program. See more in attached file. >> Maybe it is bug or maybe it should run that way. > > If you write y = x, then they are not the same variable, but they point > to (the proper Python language is they are bound to) the same object. > > Now if you say x.age = 20, then y.age will also be 20 (it's the same object). If you want y to be an independent copy of x (i.e. a new object that has the same values), use: import copy y = copy.copy(x) Now you can change x.age and it won't affect y.age -- Pieter van Oostrum www: http://pieter.vanoostrum.org/ PGP key: [8DAE142BE17999C4] -- https://mail.python.org/mailman/listinfo/python-list
Re: Can you help me solve this?
Joseph Nail writes: > Hello, > I have one problem. Somehow in my function when I wrote y=x, they are the > same variable and then it also changes age or height (which were x) in the > main program. See more in attached file. > Maybe it is bug or maybe it should run that way. If you write y = x, then they are not the same variable, but they point to (the proper Python language is they are bound to) the same object. Now if you say x.age = 20, then y.age will also be 20 (it's the same object). -- Pieter van Oostrum www: http://pieter.vanoostrum.org/ PGP key: [8DAE142BE17999C4] -- https://mail.python.org/mailman/listinfo/python-list
Re: Can you help me solve this?
Hi, On Mon, Mar 16, 2020 at 4:26 PM Joseph Nail wrote: > > Hello, > I have one problem. Somehow in my function when I wrote y=x, they are the > same variable and then it also changes age or height (which were x) in the > main program. See more in attached file. > Maybe it is bug or maybe it should run that way. This is the text only ML -> NO ATTACHMENT. Please copy and paste the code inside the E-mail body send it out. Thank you, > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Can you help me solve this?
Hello, I have one problem. Somehow in my function when I wrote y=x, they are the same variable and then it also changes age or height (which were x) in the main program. See more in attached file. Maybe it is bug or maybe it should run that way. -- https://mail.python.org/mailman/listinfo/python-list
Re: hello please can you help me with the below problem am facing
On Friday, January 9, 2015 at 3:47:27 AM UTC-8, mubarak idris wrote: Please how can I make an .exe executable app out of my python script easily http://www.py2exe.org/ -- https://mail.python.org/mailman/listinfo/python-list
hello please can you help me with the below problem am facing
Please how can I make an .exe executable app out of my python script easily -- https://mail.python.org/mailman/listinfo/python-list
Can you help me???
Are you traveling abroad on holiday??? Does the resort have enough information on the internet??? Would you like to ask local people information about attractions, good places to eat, nice places, that is, just about anything??? For this site, I have asked people around the world to join and tell the tourists information about their own country / city. Similarly, when they themselves are traveling somewhere, they can get here also interesting information about the destination. A similar service has not to my knowledge found on the internet and I would hope that you decide to join as a free member and you will help other people to make their holiday a successful :) And I also hope that if you are a self-help / information requirements, hopefully my site will also help you http://theworldismy.webs.com/ -- http://mail.python.org/mailman/listinfo/python-list
can you help me
nbsp; hello please, I have this error,nbsp;error C1083nbsp;Cannot open include file BaseTsd.h, invalide argument, I installed the platformSDKnbsp;, but the same error , can you help me __ Do You Yahoo!? En finir avec le spam? Yahoo! Mail vous offre la meilleure protection possible contre les messages non sollicités http://mail.yahoo.fr Yahoo! Mail -- http://mail.python.org/mailman/listinfo/python-list
Can you help me with the below code? Urgent!
I want to trace a function while it executes and keep its local variables as states. In trace function all the things work well but after all when I print states, they are all the same. I couldn't solve the problem. I guess it's because of frame, please can you help me? import sys states = [] def traceit(frame, event, arg): location = frame.f_code.co_name if location == afunc: states.append(frame.f_locals) print frame.f_locals return traceit def afunc(a, b, c): x = a + b y = b + c z = b rslt = x + y - z return rslt print This is what I should see! sys.settrace(traceit) afunc(1, 2, 3) sys.settrace(None) print \nWhy does this happen and how can I solve it? for s in states: print s -- http://mail.python.org/mailman/listinfo/python-list
Re: Can you help me with the below code? Urgent!
This is because in states you store a reference to frame.f_locals, not the value it takes. When you print states, all the items are the same reference to the same object and have the same value If you want to store the values at each cycle you should store a copy of frame.f_locals, which will give you a different object After import sys add the line : import copy and instead of states.append(frame.f_locals) write states.append(copy.copy(frame.f_locals)) Another example of this side-effect of storing references and not values : Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. states = [] x = [0] for i in range(10): ... x[0] = i ... states.append(x) ... print states [[9], [9], [9], [9], [9], [9], [9], [9], [9], [9]] Pierre -- http://mail.python.org/mailman/listinfo/python-list
Re: Can you help me with the below code? Urgent!
Hello, You're right about it but this is a simple code which tells my problem. I need actually the frame itself for states and unfortunately copy.copy(frame) throws an exception. Pickling also doesn't work. Do you have any other idea? Thanks, Gokce. Pierre Quentel schrieb: This is because in states you store a reference to frame.f_locals, not the value it takes. When you print states, all the items are the same reference to the same object and have the same value If you want to store the values at each cycle you should store a copy of frame.f_locals, which will give you a different object After import sys add the line : import copy and instead of states.append(frame.f_locals) write states.append(copy.copy(frame.f_locals)) Another example of this side-effect of storing references and not values : Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. states = [] x = [0] for i in range(10): ... x[0] = i ... states.append(x) ... print states [[9], [9], [9], [9], [9], [9], [9], [9], [9], [9]] Pierre -- http://mail.python.org/mailman/listinfo/python-list
Re: Can you help me with the below code? Urgent!
[EMAIL PROTECTED] a écrit : Hello, You're right about it but this is a simple code which tells my problem. I need actually the frame itself for states and unfortunately copy.copy(frame) throws an exception. Pickling also doesn't work. Do you have any other idea? Thanks, Gokce. In your original post you said you wanted to store local variables, but it seems that you need to store more information than this : could you specify which ones ? Since you can't copy the stack frame, maybe you can copy only the attributes you need Pierre -- http://mail.python.org/mailman/listinfo/python-list
Re: Can you help me with the below code? Urgent!
Hello, Thanks for your help. I just copy things that I want to keep using copy.copy function. Trying to copy frames won't lead me to anywhere :) Gokce. Pierre Quentel schrieb: [EMAIL PROTECTED] a écrit : Hello, You're right about it but this is a simple code which tells my problem. I need actually the frame itself for states and unfortunately copy.copy(frame) throws an exception. Pickling also doesn't work. Do you have any other idea? Thanks, Gokce. In your original post you said you wanted to store local variables, but it seems that you need to store more information than this : could you specify which ones ? Since you can't copy the stack frame, maybe you can copy only the attributes you need Pierre -- http://mail.python.org/mailman/listinfo/python-list