2

I have a very simple question. I have an array of floats

   a = array([0.01,0.1,10,100,1000])

I would like to print this array so that the end result looks something like

   10$^-2$, 10$^-1$, ....

Is that possible with the % command?

1
  • You should probably specify the behavior of the program for 1, as well as for floats which are not powers of 10. Commented Aug 10, 2012 at 8:56

4 Answers 4

4
a = [0.01,0.1,10,100,1000]
for x in a:
     base,exp = "{0:.0e}".format(x).split('e')
     print "{0}0$^{1}$".format(base,exp)

output:

10$^-02$
10$^-01$
10$^+01$
10$^+02$
10$^+03$
Sign up to request clarification or add additional context in comments.

3 Comments

This is 10$^+00$ for 1, but since the OP didn't specify the behavior, that seems fine. May not work for non-10 inputs, but OP didn't specify that behavior either. Maybe there is a very small chance that rounding error will mess things up, perhaps with {.0e}? But I could not trigger it.
I personally would have written it base,exp = "{0:.0e}".format(x).split('e'), then print "{0}0$^{1}$".format(base,exp)
Also this is probably not what the OP wanted because it prepends 0s, and LaTeX would probably render it as 10<sup>-02</sup>
2

as a one-liner:

["10$^{}$".format(int(math.log10(num))) for num in a]

or more clearly:

from math import *

def toLatex(powerOf10):
    exponent = int( log10(powerOf10) )
    return "10$^{}$".format(exponent)

nums = [10**-20, 0.01, 0.1, 1, 10, 100, 1000, 10**20]
[(x, toLatex(x)) for x in nums]

 

[(1e-20, '10$^-20$'),
 (0.01, '10$^-2$'),
 (0.1, '10$^-1$'),
 (1, '10$^0$'),
 (10, '10$^1$'),
 (100, '10$^2$'),
 (1000, '10$^3$'),
 (100000000000000000000L, '10$^20$')]

Comments

2

Convert the number to scientific notation string:

s = string.format("%.3e",0.001)

then replace the e+ or e- with latex format:

s.replace("e+","$^{")
s.replace("e-","$^{")

then append the latex end brackets:

s = s + "}$"

that should output:

"1.000$^{-3}$"

Comments

1

Try this:

for i in str(a):
    print i

Output:

0.01
0.1
10.0
100.0
1000.0

If you prefer scientific notation:

for i in str(a):
    print '%.3e' % i

Output:

1.000e-02
1.000e-01
1.000e+01
1.000e+02
1.000e+03

The digit in '%.3e' controls the number of digits to the right of the decimal point.

EDIT: if you want to print everything on the same line, add a comma ',' at the end of each print statement.

4 Comments

You could then replace 'e' with a '$^' by using string replacement.
Thanks for you answer, but my question might have been not clear. I would like to have my string in scientific notation but in Latex format like 10$^{-2}$ and so on
@Matteo Sorry I meant using the str.replace() function after you format the string. So maybe something like s = str.format("%.3e",num) then s.replace("e+","$")
Ah, I didn't understand from your question that you wanted LaTeX output. I thought that was just your way of expressing that you wanted scientific notation. The other answers will get you sorted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.