Python Object List (alt)

Published 2012-07-31
Please read the description.

This video is about how to create a list of unique objects in python.
The objects contain lists of values by which they can be sorted.
===========================================
For easy get-to-the-point references:
0:28 end of introduction
0:57 end of class Var explanation
1:31 end explanation of purpose
2:40 end of explanation of table generation
4:19 end of explaining t and u
4:58 end of important point
7:05 end of explaining the sorting method
8:25 end of explaining the initial problem
9:50 end of getting back to the important point (4:19)
11:23 end of commenting on the sort output
12:05 end of main content
12:35 end of the conclusion
Nothing of interest beyond that :p
===========================================
A few issues tackled in this video (though not explicitly mentioned) are:
- Creating unique objects
- Making these objects useful
- Storing them in a list
- Sorting the list of objects.

Creating unique objects is done by calling the object definition several times.
So in stead of o1 = o2 = Var() you should use o1 = Var(); o2 = Var();
Where Var is a class (see video).

Making the objects useful. Well it mostly applies to the example but I haven't been able to figure this out for a couple of days. So to me this was a problem.
Don't append values to lists in objects if you're running through a list using an iterator (such as for and while loops).
In stead, predefine the lists you'd like to store in the objects and then overwrite the lists present in the objects with those new lists.

To store the objects in a list, you may use append. You'll have to pass the identifier of the object to the proper list. So these statements work:
t = []; x = Var(); t.append(x);
or
t = []; t.append(Var())
or
t = [Var() for i in range(0,10,1)]

To sort, you've got to append the lists of objects with the value you want to sort by. So as can be seen in the output, the list of objects looks like this:
[
[obj0],
[obj1],
[obj2]
]
Then we append the values obtained from the objects to the inner lists:
[
[obj0,v0],
[obj1,v1],
[obj2,v2]
]
We can now sort that list by it's second column, which only contains values (ints, strings, floats etc). The associated objects move along. So maybe the sorted list looks like:
[
[obj2,v2],
[obj0,v0],
[obj1,v1]
]
Finally we strip the values, resolving in just a list of objects again. But sorted in this case.
============================================
Python pages: www.python.org/
Download version 3.2 for windows 64 bit:
www.python.org/ftp/python/3.2/python-3.2.amd64.msi

Check out the local tutorials. Python = programming for 12 year olds (it's that easy usually) but almost as fast as C (since it's essentially one big user friendly mask for C modules).

I'll upload the source of this code to sites.google.com/site/freehomebrew/
Where there's also other stuff I create (site is pretty fresh'n empty at the moment though).

The video on my site:
sites.google.com/site/freehomebrew/videos/python-q…

All Comments (1)