List Manipulation

Iain King iainking at gmail.com
Wed Jul 5 04:10:59 EDT 2006


Mike Kent wrote:
> Roman wrote:
> > Thanks for your help
> >
> > My intention is to create matrix based on parsed csv file.  So, I would
> > like to have a list of columns (which are also lists).
> >
> > I have made the following changes and it still doesn't work.
> >
> >
> > cnt = 0
> > p=[[], [], [], [], [], [], [], [], [], [], []]
> > reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
> >                          quotechar="'", delimiter='\t')
> > for line in reader:
> >     if cnt > 6:
> >        break
> >     j = 0
> >     for col in line:
> >        p[j].append(col)
> >        j=j+1
> >     cnt = cnt + 1
> >
> > print p
>
> p[j] does not give you a reference to an element inside p.  It gives
> you a new sublist containing one element from p.  You then append a
> column to that sublist.  Then, since you do nothing more with that
> sublist, YOU THROW IT AWAY.
>
> Try doing:
>
> p[j] = p[j].append(col)
>

No, this doesn't work.  append is an in-place operation, and you'll end
up setting p[j] to it's return, which is None.

Iain




More information about the Python-list mailing list