Format Code Repeat Counts?

J

jschwab

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

In Fortran my format strings can have repeat counts.

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

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?

Thanks,
Josiah
 
M

MRAB

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

In Fortran my format strings can have repeat counts.

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

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) + "]"
 
C

Chris Rebert

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
 
M

MRAB

Chris said:
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)
 
E

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.

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

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
 
G

Gabriel Genellina

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]'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top