[Tutor] Building Starships -- object of type 'int' has no len()
Alan Gauld
alan.gauld at btinternet.com
Mon Aug 18 03:08:48 CEST 2014
On 18/08/14 00:48, Terry--gmail wrote:
> Sorry about the HTML. I think I have it turned off now in Thunderbirdy
> for this address. If so, then what follows should not be flat. If it is
> flat, please tell me.
Still flat for me... Sorry.
> The fact is, I am VERY interested in acquiring that 'Pythonic' view you
> mention and I have encountered that term bandied about on the Internet,
> but have come away confused as to exactly what Pythonic Thinking really
> is! The writers seem to take it for granted I know. I don't.
It just means using the "normal" Python idioms - and they are learned by
reading other Python code. Gradually it permeats your brain and seems
second nature.
There are a few guidelines published but mostly its just local style.
> But I am now very curious to see how this same coding would be
> accomplished in a Pythonic Way, so, letting the statement you gave me
> redefine the entire flow of thought in that area of code--
>
> catalog2 = [
> ["Drives", "Type", "Price", "Max Speed", "Energy Drain", "Rq-Crew", "",
> "", ""],
> ["Drives", "Solar Sail", "3", "1", "None", "2", "", "", ""],
> ["Drives", "Bussard Ramjet", "10", "7", "2", "3", "", "", ""],
> ["Drives", "Fusion", "70", "15", "5", "8", "", "", ""],
> ["Drives", "Matter-Antimatter", "1500", "145", "15", "13", "", "", ""],
> ["Drives", "Warp Drive", "2500", "250", "45", "17", "", "", ""],
> ]
>
>
> We want to find the maximum size for each column--
>
> lens = [0] * len(catalog2[0])
>
> for line_number, row in enumerate(catalog2):
> for col, item in enumerate(row):
> if lens[col] < len(item):
> lens[col] = len(item)
You don't need the enumerate() for the first loop
since you aren't using the index.
for row in catalog2[1:]: # miss the header row
for col,item in enumerate(row):
if lens[col] < len(item):
lens[col] = len(item)
Another way to do it would be to store a list of
lengths for each field then get the max of each list,
like this:
lens = [] * len(catalog2[0])
for row in catalog2[1:]: # miss the first row
for col,item in row:
lens[col].append(len(item))
lens = [max(col) for col in lens]
I suspect the first one is quicker but without timing them
I'm not sure.
I'm also not sure which I'd consider most Pythonic...
> Do we anticipate an execution speed increase doing it this way also?
Sometimes, but its also usually more reliable - less chance of
dropping off the end of a row or missing one out.
> Or have we altered the way we think to fit a Pythonic Way of structuring
> that will help us with other portions of the language???
yes a bit of that.
More thinking about the intent - to process all items - than about the
how of the implementation - counting indexes up to the length.
> pattern of it become more easily readable at a glance after we have
> gotten used to it?)
That too, and also for other Python programmers.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list