I have a Dictionary of Classes where the classes hold attributes that are lists of strings.
I made this function to find out the max number of items are in one of those lists for a particular person.
def find_max_var_amt(some_person) #pass in a patient id number, get back their max number of variables for a type of variable
max_vars=0
for key, value in patients[some_person].__dict__.items():
challenger=len(value)
if max_vars < challenger:
max_vars= challenger
return max_vars
What I want to do is rewrite it so that I do not have to use the .iteritems() function. This find_max_var_amt function works fine as is, but I am converting my code from using a dictionary to be a database using the dbm module, so typical dictionary functions will no longer work for me even though the syntax for assigning and accessing the key:value pairs will be the same. Thanks for your help!
Since
dbmdoesn’t let you iterate over the values directly, you can iterate over the keys. To do so, you could modify yourforloop to look likeI think a bigger issue, though, will be the fact that
dbmonly stores strings. So you won’t be able to store the list directly in the database; you’ll have to store a string representation of it. And that means that when you try to compute the length of the list, it won’t be as simple aslen(value); you’ll have to develop some code to figure out the length of the list based on whatever string representation you use. It could just be as simple aslen(the_string.split(',')), just be aware that you have to do it.By the way, your existing function could be rewritten using a generator, like so:
and if you did it that way, the change to iterating over keys would look like