access python multi dimentional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • askalottaqs
    New Member
    • Jun 2007
    • 75

    access python multi dimentional array

    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
  • woooee
    New Member
    • Mar 2008
    • 43

    #2
    You have to use a dictionary of lists for that if I read the question correctly.
    Code:
    names_dic = {}
    names_dic["jun"] = ["rifai","hassan","ali"] 
    names_dic["sonny"] = ["omar", "khaled"] 
    names_dic["xandy"] = ["heba","zuhdi","lutfi"]
    
    print 'list associated with "sonny" is ', names_dic["sonny"]
    
    ##
    animators = ["jun","sonny","xandy"]
    for name in animators:
       if name in names_dic:
          for list_name in names_dic[name]:
             print list_name, "is accessed via", name
       else:
          print name, "is not in the dictionary"
       print
    
    ##   to print all in alphabetical order, sort the keys 
    keys_list = names_dic.keys()
    keys_list.sort()
    print keys_list

    Comment

    • askalottaqs
      New Member
      • Jun 2007
      • 75

      #3
      didnt know dictionaries cld have more than a single definition! thanks a lot mate thats exactly what i want

      Cheers :D

      Comment

      Working...