Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > associative array

Reply
Thread Tools

associative array

 
 
Javier Montoya
Guest
Posts: n/a
 
      03-31-2010
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


 
Reply With Quote
 
 
 
 
Kushal Kumaran
Guest
Posts: n/a
 
      03-31-2010
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/data...l#dictionaries

--
regards,
kushal
 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      03-31-2010
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


 
Reply With Quote
 
Javier Montoya
Guest
Posts: n/a
 
      04-01-2010
On Mar 31, 7:36*pm, Gary Herron <gher...@digipen.edu> 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
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      04-01-2010
On Thu, Apr 1, 2010 at 3:58 AM, Javier Montoya <> wrote:
> On Mar 31, 7:36Â*pm, Gary Herron <gher...@digipen.edu> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-01-2010
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
 
Reply With Quote
 
Harishankar
Guest
Posts: n/a
 
      04-01-2010
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why "associative" in associative container? desktop C++ 5 06-26-2007 07:49 AM
Array and Hash (Associative array) in JavaScript v.3.0 VK Javascript 36 07-30-2005 03:21 PM
<FAQENTRY> Array and hash (associative array) VK Javascript 47 07-13-2005 06:27 AM
How to read associative array from file? nospam Perl 2 06-26-2004 09:46 AM
[newbie]saving and reading array of associative array Yvon Thoraval Ruby 5 09-17-2003 07:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57