[Tutor] Building Starships -- object of type 'int' has no len()

Japhy Bartlett japhy at pearachute.com
Wed Aug 20 23:03:31 CEST 2014


this forms a list of integers

>>> [0]*5
[0, 0, 0, 0, 0]

what I think you want is something like:

>>> [[0] for i in range(5)]
[[0], [0], [0], [0], [0]]

(a list of lists)

>>> foo = [[0] for i in range(5)]
>>> foo[3].append('bar')
>>> foo
[[0], [0], [0], [0, 'bar'], [0]]



On Wed, Aug 20, 2014 at 3:56 PM, Marc Tompkins <marc.tompkins at gmail.com>
wrote:

> On Wed, Aug 20, 2014 at 1:38 PM, Terry--gmail <terry.kemmerer at gmail.com>
> wrote:
> > Marc, my understanding is, is that:
> >
> >     lens[col].append(len(item))
> >
> > -should be building a mirror image of my list of lists called catalog2,
> > which currently has 9 columns by x number of rows, and that we are
> plugging
> > into these positions, the sizes of all the elements in that block of
> data.
>
> What's important is how you defined lens:
> >  lens = [0] * len(catalog2[0])
>
> That's a list of integers, as far as I can tell without running it
> (I'm away from an interpreter at the moment.)  So no, you cannot do
> lens[col].append(whatever), because - as the error message said -
> 'int' object has no attribute 'append'.
>
> There might be some misunderstanding about what list.append(whatever)
> does - it adds "whatever" to the end of list.  It does NOT assign
> values to elements that already exist; to do that, you need to do
> assignment by index.  So maybe this is what you're looking for?:
> >     lens[col] = len(item)
>
>
>
> > So, I completely don't understand why we would eliminate the positioning
> of
> > which list we are referencing in lens by saying:
> >
> >     lens.append(len(item))
> >
> > It seems to me that, that statement would put the entire block of element
> > sizes into one list, and the next MAX statement would then yield only a
> > single number, which would be the largest size element it encounted in
> the
> > whole of catalog2!
> >
> > Or am I really missing the boat here? :)
>
> >     lens.append(len(item))
> will append a single integer to lens.  I'm afraid I don't quite follow
> the bit about "the next MAX statement", as I've lost the thread of
> what your larger project is trying to accomplish...  In any case,
> max() _should_ only return a single number, no?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140820/f9d17e69/attachment-0001.html>


More information about the Tutor mailing list