'generating' a print statement
David Broadwell
anti-spam.dbroadwell at mindspring.com
Thu May 8 12:11:39 EDT 2003
Myles mailed so;
>> def wideprint(temp):
>> format = '"
> Oops - you left off a closing quote mark there.
My source file has it, must have accidentally wacked it with the comment.
> repace last line with
> print format % eval(format2)
> (format2 is still a string representation of a tuple, not a tuple as
> required)
Eval ... I did try a few things there to do it ... Arg, so close.
<In a block Myles wrote:>
Or you could do something like:
def wideprint(temp):
for eachItem in temp:
print "%s " % eachItem,
print
Don't forget the closing comma after eachItem,
which tells print not to insert a newline - it does,
however, print a space.
And, of course, if the items in temp are not strings,
that would be
print "%s " % str(eachItem),
or even:
def wideprint(temp):
for eachItem in temp:
print "%s" % eachItem, " ",
print
or maybe:
def wideprint(temp):
format = "%s " * len(temp)
print format % tuple(temp)
</block>
And 'temp' is a ... hey no variables one liner! (this is why i like python)
print (len(temp) * '%s ') % tuple(temp)
Hm, time to read about tuples ... i was doing it the manual way. Well you'd
better think i'm not forgetting this tidbit. I KNEW my green was showing
through.
Thanks, Myles.
In the spagetti David Eppstein <eppstein at ics.uci.edu> wrote:
> Is this some kind of anti-Perl joke? You're building up strings that
> look like expressions, and you forgot to convert back to actual
> expressions at the end, I think you want
> print eval(format1) % eval(format2)
Funny thing is no need to eval the first format string ... And no, i don't
know perl. Were that it was. I built that purely to demonstrate exactly what
step i was missing. i KNEW i was missing one ...
> ... skip all the format cruft.
If you are green and you know it clap your hands ...
Thanks David!
"Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in the thread;
> Take this in two steps. temp is a sequence, and you want to pass all
> members of that sequence in a tuple as the right hand argument of the
> format operator. That's easy:
> format % tuple(temp)
Hm tuple, magic word.
> The left hand format string seems to be asking for %s formatting for
> each argument with two spaces separating each %s. Instead of a for loop,
> try using multiplication to repeat the string:
Actually, i'm using tabs, but for sake of the argument, i went with spaces.
Yeah, forgetting about string multiplication ...late nights and caffiene
will do that to ya.
(Of course late nights and caffiene is whaen i get my most coding done too)
> either:
> format = " ".join(['%s'] * len(temp))
Interesting abstraction.
> or:
> format = ("%s " * len(temp)).rstrip()
And have to read string.rstrip()
Thanks Duncan.
"Alex Martelli" <aleax at aleax.it> wrote in shredded garment:
> If these specs are indeed correct, another alternative way to implement
> them is to forego the % operator in favour of a direct application of
> str to all items, followed by a join of the resulting strings:
>
> ' '.join(map(str, temp))
>
> should be equivalent to the two statements that build and then use the
> format string, and it may be considered a bit clearer and more concise.
I figured this would re-factor to one line and i even saw that one line with
the first responce ... but that ... that's magic with builtins.
Thanks (again) Alex.
--
David Broadwell
(Did you know a spell checker tries to replace Eval with Evil and Tuple with
Topple! *lol*)
More information about the Python-list
mailing list