[Tutor] Creating a list of lists
Doug Stanfield
DOUGS@oceanic.com
Sun, 23 Jan 2000 22:26:42 -1000
Go to the interpreter and do some experimenting. My session is below:
[dougs@lawelawe dougs]$ python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16) [GCC pgcc-2.91.60 19981201
(egcs-1.1.1 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> list1 = [1,2,3]
>>> list2 = [2,3,4]
>>> list3 = [3,4,5]
>>> the_list = []
>>> the_list.append(list1)
>>> the_list.append(list2)
>>> the_list.append(list3)
>>> the_list
[[1, 2, 3], [2, 3, 4], [3, 4, 5]]
>>>
That will translate into more or less the following when you try to put into
use:
...whatever preliminary stuff is required to read the file...
import string
the_outside_list = [] # this will be the final product
# assuming 'lines' might be the output of a readlines()
# someplace above, a list of strings that represent each line of
# the input from the file. The statement below fills 'each'
# with a single line for further processing.
for each in lines:
columns = string.split(each,';') # the output of the split method is a
list
the_outside_list.append(columns) # append the list from each line
You'll end up with a list of lists as desired.
-Doug-
> -----Original Message-----
> From: Ben Beuchler [mailto:insyte@emt-p.org]
> Sent: Sunday, January 23, 2000 9:07 PM
> To: Python Tutor
> Subject: [Tutor] Creating a list of lists
>
>
> I will be working with a ';' delimited text file with
> customer information
> on each line. I would like to treat it as a 'list of lists'
> as it where.
>
> For some reason, I can't seem to come up with a way to add a list to a
> list as a list. ;-) I hope that didn't make sense to you, because it
> doesn't to me.
>
> What I can't figure out how to accomplish is combining lists inside
> another list without it just becoming smashed together in one
> big list.
>
> [1,2,3] and [4,5,6] should become [[1,2,3], [4,5,6]] not
> [1,2,3,4,5,6].
>
> Thanks for any direction...
>
> Ben
>
> --
> "There is no spoon"
> -- The Matrix
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>