[Tutor] Request for help learning the right way to deal with lists in lists

Eric Hamiter ehamiter at gmail.com
Tue Jul 13 03:45:03 CEST 2010


I'm fairly new to programming and Python as well, but I have a suggestion
that may be worth looking into-- are you familiar with pickling? It sounds
like something that may fit in well with what you're trying to do.

Good reference article:

http://articles.techrepublic.com.com/5100-10878_11-1052190.html


On Mon, Jul 12, 2010 at 5:19 PM, Siren Saren <siren99 at yahoo.com> wrote:

> I'm still fairly new to programming.  Python is my first language and I am
> teaching myself as best I can.  I'm struggling with a situation that I
> expect must come up all the time.  I can come up with relatively complicated
> solutions but I wonder if there's not a more pythonic way of doing it.
>
> I've seen a lot of examples in books for dealing with lists of alternating
> data types, but what about a list that doesn't follow a simple numeric
> pattern?  For example, say I have a list that's a composite of two elements:
> books and key pages / albums and favorite tracks / medicines and times
> taken, whatever.  To make a program that does something to the first set of
> elements based on the second set of elements, what kind of structure should
> I set up?
>
> Probably easier to choose one of these.  So pretend I have a list like
> this:
>
> (Crime and punishment, page 10, page 40, page 30, Brother's Karamazov, page
> 22, page 55, page 9000, Father's and Sons, page 100, Anna Karenina, page 1,
> page 2, page 4, page 7, page 9)
>
> Since I can identify the elements and since I know the values are 'in
> order,' in other words the page numbers between the first and second book
> all belong to the first book, I can make a mapping.  But I've been surprised
> at the complexity.  So in this hypothetical, with a regular expression, I
> can easily convert the pages to integers, and identify the two lists.  But
> what's the right way to map them to each other, if I am planning to, for
> example, tear out these key pages and make a wall hanging.  (I would never
> do this with precious books like these, of course).  Am I right to think
> that I want to get them into a form that clearly relates them to each other
> from the outset?  Does a dictionary make sense-- I've read that I should
> expect to put a lot of my data into dictionaries?
>
> My tentative approach has been as follows:
>
> a. Make a sublist of the Books.  Here we could just get the non-integers so
> Books = ('C and P', 'Brothers K' ...)
> b. Look each up book in the main list to get an index values
> c.  Now my approach becomes ugly.  In pseudo code-
>
> For book in Books:
>     A dictionary should map the book to a list of all the elements in the
> main list that fall between the book's index value and the next book's index
> value
>
> I keep coming up with embedded loops to express this but I simultaneously
> feel like I am missing a third layer (somehow maybe it's 'for book,' 'for
> index,' 'for element'?) and like Occham is going to come by with his razor
> and laugh at me and say, "oh there's a function that does this called the
> "one to many mapping function."
>
> I think I'm reading the right books and going to the right web pages and
> such to learn, but in this case, I must have just not comprehended.  Would
> be grateful for any input.  Have enjoyed reading the archives of this group
> as I've been trying to get my head around programming.  Thanks again
>
> Soren
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100712/ced8ec5b/attachment.html>


More information about the Tutor mailing list