[Tutor] Pythonese/Efficiency/Generalese critique please
leec03273 at mac.com
Sun Jun 5 18:50:01 CEST 2005
OK Kent - got it:
My little formating function could be written as (tested)
def cellpos(pname, alvl, blvl, clvl):
# breakout path components into list
pl = pname.split('/')
# insert empty cells for repeated names and
# add new path components (cells) to csvline
csvline = '"",'*(alvl - blvl) + '"' + '","'.join(pl
[alvl:clvl]) + '",'
"So, it is good to know about join(), but write the code the way that
is clearest to you."
One of my thoughts in posting this exercise is to first understand
alternatives before I decide what I like best :~)
Your points, together with Javier's points on presentation and
thoroughness, and Liam's point on making the logic more obvious are
Thank you all for taking the time to offer your comments,
On Jun 5, 2005, at 5:47 AM, Kent Johnson wrote:
> Lee Cullens wrote:
> OK, let's try to get this right.
> Given a list of strings and a divider string, join() returns the
> strings from the list 'joined' by the divider string. For example,
>>>> pl = ['a', 'b', 'c']
> The syntax for this takes a little getting used to; join() is
> actually a string method; you call it on the divider string and
> pass it the list of strings to be joined.
> To make a list of quoted, comma separated values, you also need
> initial and final quotes:
>>>> '"' + '","'.join(pl) + '"'
> In your example you need '","'.join(pl[0:2])
>> Saving one line of code in this module doesn't mean much, but if it
>> were a efficiency in processing it would?
> Conventional wisdom is that it is more efficient to use the join
> method. More enlightened wisdom says, don't optimize until you know
> you have a problem, and the only way to know what is fastest in
> your program with your data is to test. And of course working code
> always beats broken code :-)
> I did some experiments with these alternatives and concluded that
> if the length of the resulting string is less than about 500-800,
> the version using += is faster than the one using join():
> So, it is good to know about join(), but write the code the way
> that is clearest to you.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor