associative array

Discussion in 'Python' started by Javier Montoya, Mar 31, 2010.

  1. Dear all,

    I'm a newbie in python and would be acknowledge if somebody could shed
    some light on associative arrays.
    More precisely, I would like to create a multi-dimensional associative
    array. I have for example a list of students which are identified
    uniquely by their student IDs. Additionally, for each student I have
    some information: FirstName, LastName, etc.

    The array would have then the following form:
    [StudentID] => [FirstName][LastName][Telephone]...[ ... ]

    I would like to be able to access a field directly by using a
    StudentID
    [StudentID][FirstName]
    [StudentID][LastName]

    How could I manipulate such an array (create the array, add elements,
    access data)?

    Best wishes
     
    Javier Montoya, Mar 31, 2010
    #1
    1. Advertising

  2. On Wed, Mar 31, 2010 at 10:10 PM, Javier Montoya <> wrote:
    > Dear all,
    >
    > I'm a newbie in python and would be acknowledge if somebody could shed
    > some light on associative arrays.
    > More precisely, I would like to create a multi-dimensional associative
    > array. I have for example a list of students which are identified
    > uniquely by their student IDs. Additionally, for each student I have
    > some information: FirstName, LastName, etc.
    >
    > The array would have then the following form:
    > [StudentID] => [FirstName][LastName][Telephone]...[ ... ]
    >
    > I would like to be able to access a field directly by using a
    > StudentID
    > [StudentID][FirstName]
    > [StudentID][LastName]
    >
    > How could I manipulate such an array (create the array, add elements,
    > access data)?
    >


    Did you try the python tutorial? Dictionaries might be what you are
    looking for.

    http://docs.python.org/tutorial/datastructures.html#dictionaries

    --
    regards,
    kushal
     
    Kushal Kumaran, Mar 31, 2010
    #2
    1. Advertising

  3. Javier Montoya

    Gary Herron Guest

    Javier Montoya wrote:
    > Dear all,
    >
    > I'm a newbie in python and would be acknowledge if somebody could shed
    > some light on associative arrays.
    > More precisely, I would like to create a multi-dimensional associative
    > array. I have for example a list of students which are identified
    > uniquely by their student IDs. Additionally, for each student I have
    > some information: FirstName, LastName, etc.
    >
    > The array would have then the following form:
    > [StudentID] => [FirstName][LastName][Telephone]...[ ... ]
    >
    > I would like to be able to access a field directly by using a
    > StudentID
    > [StudentID][FirstName]
    > [StudentID][LastName]
    >
    > How could I manipulate such an array (create the array, add elements,
    > access data)?
    >
    > Best wishes
    >
    >
    >

    Create a class for student with attributes for ID, FirstName, LastName, etc.

    class Student:
    def __init__(self, id, FirstName, ...):
    self.id = id
    self.FirstName = FirstName
    ...

    then whenever you create a student object, use a dictionary to associate
    the object with its is
    AA = {} # An empty dictionary
    s = Student(...)
    AA[s.id] = s
    ... and repeat for many students...

    Then to access a student's object given an id:
    s = AA[id]
    print s.id, s.FirstName, s.LastName, ...


    I'd *not* call this a multi-dimension association, but rather just an
    association between student objects and their ids.

    Hope that helps,

    Gary Herron

    --
    Gary Herron, PhD.
    Department of Computer Science
    DigiPen Institute of Technology
    (425) 895-4418
     
    Gary Herron, Mar 31, 2010
    #3
  4. On Mar 31, 7:36 pm, Gary Herron <> wrote:
    > JavierMontoyawrote:
    > > Dear all,

    >
    > > I'm a newbie in python and would be acknowledge if somebody could shed
    > > some light on associative arrays.
    > > More precisely, I would like to create a multi-dimensional associative
    > > array. I have for example a list of students which are identified
    > > uniquely by their student IDs. Additionally, for each student I have
    > > some information: FirstName, LastName, etc.

    >
    > > The array would have then the following form:
    > > [StudentID] => [FirstName][LastName][Telephone]...[ ... ]

    >
    > > I would like to be able to access a field directly by using a
    > > StudentID
    > > [StudentID][FirstName]
    > > [StudentID][LastName]

    >
    > > How could I manipulate such an array (create the array, add elements,
    > > access data)?

    >
    > > Best wishes

    >
    > Create a class for student with attributes for ID, FirstName, LastName, etc.
    >
    >   class Student:
    >       def __init__(self, id, FirstName, ...):
    >           self.id = id
    >           self.FirstName = FirstName
    >           ...
    >
    > then whenever you create a student object, use a dictionary to associate
    > the object with its is
    >   AA = {} # An empty dictionary
    >   s = Student(...)
    >   AA[s.id] = s
    >   ... and repeat for many students...
    >
    > Then to access a student's object given an id:
    >   s = AA[id]
    >   print s.id, s.FirstName, s.LastName, ...
    >
    > I'd *not* call this a multi-dimension association, but rather just an
    > association between student objects and their ids.
    >
    > Hope that helps,
    >
    > Gary Herron
    >
    > --
    > Gary Herron, PhD.
    > Department of Computer Science
    > DigiPen Institute of Technology
    > (425) 895-4418


    Dear all,

    Thanks for your suggestions, it worked! As Gary suggested, I created a
    'student' class and mapped its objects to a dictionary.
    Is it possible to sort the dictionary by the student's grades in
    descending order?

    Best wishes
     
    Javier Montoya, Apr 1, 2010
    #4
  5. Javier Montoya

    Chris Rebert Guest

    On Thu, Apr 1, 2010 at 3:58 AM, Javier Montoya <> wrote:
    > On Mar 31, 7:36 pm, Gary Herron <> wrote:
    >> JavierMontoyawrote:
    >> > Dear all,

    >>
    >> > I'm a newbie in python and would be acknowledge if somebody could shed
    >> > some light on associative arrays.
    >> > More precisely, I would like to create a multi-dimensional associative
    >> > array. I have for example a list of students which are identified
    >> > uniquely by their student IDs. Additionally, for each student I have
    >> > some information: FirstName, LastName, etc.

    >>
    >> > The array would have then the following form:
    >> > [StudentID] => [FirstName][LastName][Telephone]...[ ... ]

    >>
    >> > I would like to be able to access a field directly by using a
    >> > StudentID
    >> > [StudentID][FirstName]
    >> > [StudentID][LastName]

    >>
    >> > How could I manipulate such an array (create the array, add elements,
    >> > access data)?

    >>
    >> > Best wishes

    >>
    >> Create a class for student with attributes for ID, FirstName, LastName, etc.
    >>
    >>   class Student:
    >>       def __init__(self, id, FirstName, ...):
    >>           self.id = id
    >>           self.FirstName = FirstName
    >>           ...
    >>
    >> then whenever you create a student object, use a dictionary to associate
    >> the object with its is
    >>   AA = {} # An empty dictionary
    >>   s = Student(...)
    >>   AA[s.id] = s
    >>   ... and repeat for many students...
    >>
    >> Then to access a student's object given an id:
    >>   s = AA[id]
    >>   print s.id, s.FirstName, s.LastName, ...
    >>
    >> I'd *not* call this a multi-dimension association, but rather just an
    >> association between student objects and their ids.
    >>
    >> Hope that helps,
    >>
    >> Gary Herron
    >>
    >> --
    >> Gary Herron, PhD.
    >> Department of Computer Science
    >> DigiPen Institute of Technology
    >> (425) 895-4418

    >
    > Dear all,
    >
    > Thanks for your suggestions, it worked! As Gary suggested, I created a
    > 'student' class and mapped its objects to a dictionary.
    > Is it possible to sort the dictionary by the student's grades in
    > descending order?


    Dictionaries are not ordered collections, so they themselves cannot be sorted.

    However, if you want a list of students sorted in the manner you stated:
    class_rank = list(garys_dictionary.values())
    class_rank.sort(key=lambda student: student.grade, reverse=True)

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Apr 1, 2010
    #5
  6. Javier Montoya

    Peter Otten Guest

    Javier Montoya wrote:

    > Is it possible to sort the dictionary by the student's grades in
    > descending order?


    You cannot sort dictionaries, but you can put dictionary items into a list
    and then sort that.

    Assumming that you have a dictionary student_dict that maps student IDs to
    Student instances and a function get_grade() to find a student's grade you'd
    do

    def get_grade(student):
    return course[student.student_id].grade # or whatever it takes

    students_by_grade = sorted(student_dict.itervalues(), key=get_grade,
    reverse=True)

    for student in students_by_grade:
    print student.first_name, student.last_name

    However, problems like the above are normally handled best using a SQL
    database, so I recommend that you have a look at

    http://docs.python.org/library/sqlite3.html

    Peter
     
    Peter Otten, Apr 1, 2010
    #6
  7. Javier Montoya

    Harishankar Guest

    On Wed, 31 Mar 2010 09:40:30 -0700, Javier Montoya wrote:

    > Dear all,
    >
    > I'm a newbie in python and would be acknowledge if somebody could shed
    > some light on associative arrays.
    > More precisely, I would like to create a multi-dimensional associative
    > array. I have for example a list of students which are identified
    > uniquely by their student IDs. Additionally, for each student I have
    > some information: FirstName, LastName, etc.
    >
    > The array would have then the following form: [StudentID] =>
    > [FirstName][LastName][Telephone]...[ ... ]
    >
    > I would like to be able to access a field directly by using a StudentID
    > [StudentID][FirstName]
    > [StudentID][LastName]
    >
    > How could I manipulate such an array (create the array, add elements,
    > access data)?
    >
    > Best wishes


    I know this is not a direct answer, but in your case I would probably use
    a database, because it is the easiest and most standardized way to access
    such types of data. If you don't want something heavyweight, use sqlite
    with in-memory databases/persistent file.

    Look at sqlite3 module in python.





    --
    Harishankar (http://harishankar.org http://literaryforums.org)
     
    Harishankar, Apr 1, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nospam
    Replies:
    2
    Views:
    646
    Mohammd M. Hussain
    Jun 26, 2004
  2. desktop
    Replies:
    5
    Views:
    405
    James Kanze
    Jun 26, 2007
  3. Yvon Thoraval
    Replies:
    5
    Views:
    228
    Jason Creighton
    Sep 17, 2003
  4. VK
    Replies:
    47
    Views:
    589
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    684
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page