Re: Format Code Repeat Counts?

2009-08-14 Thread Scott David Daniels

MRAB wrote:

Scott David Daniels wrote:

MRAB wrote:

The shortest I can come up with is:
"[" + "][".join(letters) + "]"


Maybe a golf shot:
  "][".join(letters).join("[]")


Even shorter:

"["+"][".join(letters)+"]"

:-)

I was going by PEP8 rules. ;-)

--Scott David Daniels
Scott David dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Format Code Repeat Counts?

2009-08-14 Thread MRAB

Scott David Daniels wrote:

MRAB wrote:

The shortest I can come up with is:
"[" + "][".join(letters) + "]"


Maybe a golf shot:
  "][".join(letters).join("[]")


Even shorter:

"["+"][".join(letters)+"]"

:-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Format Code Repeat Counts?

2009-08-14 Thread jschwab
Thanks all! That was most helpful and informative.

Best,
Josiah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Format Code Repeat Counts?

2009-08-13 Thread Scott David Daniels

MRAB wrote:

The shortest I can come up with is:
"[" + "][".join(letters) + "]"


Maybe a golf shot:
  "][".join(letters).join("[]")


--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Format Code Repeat Counts?

2009-08-12 Thread Gabriel Genellina
En Wed, 12 Aug 2009 19:23:32 -0300, Chris Rebert   
escribió:



On Wed, Aug 12, 2009 at 4:34 PM, jschwab wrote:


As a more concrete example, say I have several sets of letters in a
list of strings
    letters = ["aeiou", "hnopty", "egs", "amsp"]
and I wanted to build a regular expression string out of them like
    re_str <==> "[aeiou][hnopty][egs][amsp]"
Right now, the best I've got that doesn't require an explicit string
like "[{1}][{2}][{3}][{4}]" is
    re_str = "".join(map(lambda x: "[{0}]".format(x), letters))

Is there a better way?


Slightly better, by using a generator expression instead of map() and  
lambda:

re_str = "".join("[{0}]".format(x) for x in letters)

Though obviously MRAB's is shorter (and a good show of lateral thinking).


Another way, using {} auto-numbering (requires Python 3.1 or the future  
2.7)


p3> letters = ["aeiou", "hnopty", "egs", "amsp"]
p3> ("[{}]"*len(letters)).format(*letters)
'[aeiou][hnopty][egs][amsp]'


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Re: Format Code Repeat Counts?

2009-08-12 Thread Emile van Sebille

On 8/12/2009 1:34 PM jschwab said...

Are repeat counts supported Python's str.format() in some fashion?

In Fortran my format strings can have repeat counts.


write(*, fmt="3F8.3") [1, 2, 3]
   1.000  2.000   3.000


I don't think printf-style format codes, which is what'd I'd
previously used in Python, allow for repeat counts.

As a more concrete example, say I have several sets of letters in a
list of strings
 letters = ["aeiou", "hnopty", "egs", "amsp"]
and I wanted to build a regular expression string out of them like
 re_str <==> "[aeiou][hnopty][egs][amsp]"
Right now, the best I've got that doesn't require an explicit string
like "[{1}][{2}][{3}][{4}]" is
 re_str = "".join(map(lambda x: "[{0}]".format(x), letters))

Is there a better way?



I don't know.  I often end up at something like:

"[%s]"*len(letters) % tuple(letters)

Emile

--
http://mail.python.org/mailman/listinfo/python-list


Re: Format Code Repeat Counts?

2009-08-12 Thread MRAB

Chris Rebert wrote:

On Wed, Aug 12, 2009 at 4:34 PM, jschwab wrote:


As a more concrete example, say I have several sets of letters in a
list of strings
letters = ["aeiou", "hnopty", "egs", "amsp"]
and I wanted to build a regular expression string out of them like
re_str <==> "[aeiou][hnopty][egs][amsp]"
Right now, the best I've got that doesn't require an explicit string
like "[{1}][{2}][{3}][{4}]" is
re_str = "".join(map(lambda x: "[{0}]".format(x), letters))

Is there a better way?


Slightly better, by using a generator expression instead of map() and lambda:
re_str = "".join("[{0}]".format(x) for x in letters)

Though obviously MRAB's is shorter (and a good show of lateral thinking).


Python 3.1 supports auto-numbered placeholders "[{}]", so:

re_str = ("[{}]" * len(letters)).format(*letters)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Format Code Repeat Counts?

2009-08-12 Thread Chris Rebert
On Wed, Aug 12, 2009 at 4:34 PM, jschwab wrote:

> As a more concrete example, say I have several sets of letters in a
> list of strings
>     letters = ["aeiou", "hnopty", "egs", "amsp"]
> and I wanted to build a regular expression string out of them like
>     re_str <==> "[aeiou][hnopty][egs][amsp]"
> Right now, the best I've got that doesn't require an explicit string
> like "[{1}][{2}][{3}][{4}]" is
>     re_str = "".join(map(lambda x: "[{0}]".format(x), letters))
>
> Is there a better way?

Slightly better, by using a generator expression instead of map() and lambda:
re_str = "".join("[{0}]".format(x) for x in letters)

Though obviously MRAB's is shorter (and a good show of lateral thinking).

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Format Code Repeat Counts?

2009-08-12 Thread MRAB

jschwab wrote:

Are repeat counts supported Python's str.format() in some fashion?

In Fortran my format strings can have repeat counts.


write(*, fmt="3F8.3") [1, 2, 3]
   1.000  2.000   3.000


I don't think printf-style format codes, which is what'd I'd
previously used in Python, allow for repeat counts.

As a more concrete example, say I have several sets of letters in a
list of strings
 letters = ["aeiou", "hnopty", "egs", "amsp"]
and I wanted to build a regular expression string out of them like
 re_str <==> "[aeiou][hnopty][egs][amsp]"
Right now, the best I've got that doesn't require an explicit string
like "[{1}][{2}][{3}][{4}]" is
 re_str = "".join(map(lambda x: "[{0}]".format(x), letters))

Is there a better way?


The shortest I can come up with is:

"[" + "][".join(letters) + "]"

--
http://mail.python.org/mailman/listinfo/python-list