By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

access python multi dimentional array

P: 75
To make the q simple: i want to access inner array elements by the names of the inner arrays. thanks

here's a more complex explanation:

I have those arrays:

animatorsNames = ["jun","sonny","xandy"]

jun = ["rifai","hassan","ali"]
sonny = ["omar", "khaled"]
xandy = ["heba","zuhdi","lutfi"]

animators = [jun,sonny,xandy]

I want to go over the array with loops like this:

for scene in animatorsNames :
for animator in scene:
do something


th problem is that i want to access the array elements through the names of those inner arrays, jun, sunny, etc. but when i go over the second loops, it treats the animator variable as a variable, rather than an array. which is the right thing for it to do. but how would i be able to achieve it to go over those variables as if they are the inner arrays.

I hope it was clear, any help is appreciated.

thanks a lot ppl :D
May 11 '08 #1
Share this Question
Share on Google+
2 Replies


P: 43
You have to use a dictionary of lists for that if I read the question correctly.
Expand|Select|Wrap|Line Numbers
  1. names_dic = {}
  2. names_dic["jun"] = ["rifai","hassan","ali"] 
  3. names_dic["sonny"] = ["omar", "khaled"] 
  4. names_dic["xandy"] = ["heba","zuhdi","lutfi"]
  5.  
  6. print 'list associated with "sonny" is ', names_dic["sonny"]
  7.  
  8. ##
  9. animators = ["jun","sonny","xandy"]
  10. for name in animators:
  11.    if name in names_dic:
  12.       for list_name in names_dic[name]:
  13.          print list_name, "is accessed via", name
  14.    else:
  15.       print name, "is not in the dictionary"
  16.    print
  17.  
  18. ##   to print all in alphabetical order, sort the keys 
  19. keys_list = names_dic.keys()
  20. keys_list.sort()
  21. print keys_list
May 11 '08 #2

P: 75
didnt know dictionaries cld have more than a single definition! thanks a lot mate thats exactly what i want

Cheers :D
May 11 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.