ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi. I was wondering if anyone could help me with this problem.
I have a list of integers, i=[i1, i2, ..., in], and a dictionary, d, and I want to write some code that will assign a value, x, to d[i1][i2]...[in], the difficulty is that this code will be run repeatedly with different length i's.
Ok, so my actual problem involves nested for loops.
Say I have three nested for loops, and do something like
Code:
d={}
for i1 in range(something):
d[i1]={}
for i2 in range(something):
d[i1][i2]={}
for i3 in range(something):
d[i1][i2][i3]=x
Now, in my program I want to be able to decide how many nested for loops I have at runtime, so I define the same thing recursively. The problem is that I need some code that generalises all of the above assignments, i.e., d[i1]={}, d[i1][i2]={} and d[i1][i2][i3]=x. In my case I have a list of counters [i1,i2,...,in] and want some way of recovering d[i1][i2]...[in] from this.
I'm sorry if this explanation is a bit confusing - I'm not sure how best to describe the problem!
Yeah, sorry, but I still don't follow. It might be better if you explain your desired goal, rather than your intended solution to the goal, as there could be a better way.
Also, just something to take note of, but because dictionaries aren't sequential, you can't create double-subscripted dictionaries like you could lists or tuples. However, you could probably create a dictionary within a dictionary to basically acheieve the same thing, as that is what a multi-dimensional list/tuple is anyway.
The output of my program is a multi-dimensional dictionary, but the dimension is only decided at runtime - it could be 2, 3, 4, ... etc. - and I need a generic way to assign a value to this dictionary regardless of the dimension. I.e., if it turns out to be two dimensional then I need something like d[i1][i2]=x, if it's three dimensional I need d[i1][i2][i3]=x, and so on.
Also, I'm not quite sure what you mean about not being able to create double subscripted dictionaries. I would have something like myDict={1:{1:a, 2:b}, 2:{1:c, 2:d}}, in which case I could (I'm pretty sure) assign x in place of, say, c with the statement myDict[2][1]=x.
It probably seems a bit obtuse to be using dictionaries instead of lists, but there is a reason for it which is far to boring to go in to!
That works if all the dimensions are the same. If that is not the case I've a second solution (which I don't like, but probably works): Create a string with the assignment and then use the exec() command.
Code:
def i2s(indexes):
'''Converts a list of indexes into a "mutldimentional dictionary key string'''
s = ""
for i in indexes: s += "["+str(i)+"]" # This is not efficient in python!
return s
.
.
.
indexes = [1,3,4,7,2] # This's your list of indexes
s = "mydict"+i2s(indexes)+" = x" # Here we create a string s = "mydict[1][3][4][7][2] = x"
exec(s) # And we execute it!
Originally posted by enemorales That works if all the dimensions are the same. If that is not the case I've a second solution (which I don't like, but probably works): Create a string with the assignment and then use the exec() command.
Code:
def i2s(indexes):
'''Converts a list of indexes into a "mutldimentional dictionary key string'''
s = ""
for i in indexes: s += "["+str(i)+"]" # This is not efficient in python!
return s
.
.
.
indexes = [1,3,4,7,2] # This's your list of indexes
s = "mydict"+i2s(indexes)+" = x" # Here we create a string s = "mydict[1][3][4][7][2] = x"
exec(s) # And we execute it!
Yes, this is the sort of thing that I need. I was hoping there might be some kind of builtin function or method that would do the job, but I guess there isn't. I guess I may have to come up with a different method altogether because, as you say, this won't be very efficient and I'm going to need to repeat this bit of code a lot of times.
Well, I don't know exactly which problem you are trying to solve, but maybe you can use only one dictionary an "encode" the indexes in the key. Something like
a["1-3-6-3-6"] <---> a[1][3][6][3][6]
This could easily become a mess (to keep consistency), but may lead to another type of solution to your problem...
Originally posted by enemorales Well, I don't know exactly which problem you are trying to solve, but maybe you can use only one dictionary an "encode" the indexes in the key. Something like
a["1-3-6-3-6"] <---> a[1][3][6][3][6]
This could easily become a mess (to keep consistency), but may lead to another type of solution to your problem...
If you want to have a multi-part key, you can add the values to a tuple instead of pasting them in a string:
[CODE]
a[(last_name, first_name)] = ssn
[CODE]
This has the advantage that it is easy to create the key, and it is easy to extract the individual values from the key using tuple unpacking.
Originally posted by carl.waldbieser If you want to have a multi-part key, you can add the values to a tuple instead of pasting them in a string:
[CODE]
a[(last_name, first_name)] = ssn
[CODE]
This has the advantage that it is easy to create the key, and it is easy to extract the individual values from the key using tuple unpacking.
Thanks for all the suggestions, I think this could possibly work. I'm going to have a try now and see if I can change my code to use tuple keys.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.