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

Terry--gmail terry.kemmerer at gmail.com
Thu Aug 21 00:34:27 CEST 2014


Hi Marc Tompkins!

You are absolutely right that

lens = [0] * len(catalog2[0])

Just creates a list of integers!

Here is what happened, my original method of finding the maximum element 
sizes in the 9 x ? block of data held in catalog2, only needed a final 
list of integers to contain it. However, as everyone helped me make my 
routine more pythonic, things evolved, but we always used the same 
beginning formatting statement until Alan Gauld gave me, as a last 
example, a different way to do the same thing, but he left out, I think, 
a matching formatting statement, and I ASSUMED WE WERE STILL USING THE 
OLD ONE.

You drew my eyes to it. So, I replaced the above statement with this line:

lens = [[], [], [], [], [], [], [],[], []]

So, trying that:

lens = [[], [], [], [], [], [], [],[], []]
for row in catalog2:
     for col, item in enumerate(row):
         lens[col].append(len(item))
lens = [max(col) for col in lens]
print(lens)

My result is:

[6, 17, 5, 9, 12, 7, 0, 0, 0]          <<--------THAT IS CORRECT!  IT 
WORKS!!!

But, when I try to automate the formatting of lens so that my data block 
can easily be changed in the originating table:

lens = [[]] * len(catalog2[0])
for row in catalog2:
     for col, item in enumerate(row):
         lens[col].append(len(item))
lens = [max(col) for col in lens]
print(lens)

My result is:


[17, 17, 17, 17, 17, 17, 17, 17, 17]      <<--------THAT IS NOT CORRECT!

I haven't figured out what the difference is between the two formatting 
statements....but it appears to be profound! Ha!

And the MAX statement that I was referring to that you couldn't see was 
the last line where the column lists were used to find the maximum size 
of each column of sizes and change lens to a single list of integers:

lens = [max(col) for col in lens]

--Terry














IT WORKS!

I just don't know how to automatically format lens with enough [] like I 
was able to tell







On 08/20/2014 02:56 PM, Marc Tompkins 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?
>



More information about the Tutor mailing list