0

I have a numpy array:

x = np.array([[1.5,1.3],[2.5,1.5]])

What is the most pythonic way to convert this into a list of strings like this:

y = ['1.5 1.3','2.5 1.5']

What I do now is not very effective:

y = []
for i in range(x.shape[0]):
    txt = ''
    for j in range(x.shape[1]):
       txt+=' '%.5e'%x[i,j]
    y.append(txt)

Have tried with:

x = x.astype('|S10').tolist()
y = [' '.join(i) for i in x]

but if I have a lot of decimals in x, it truncates it after the 10th decimal.

1
  • I found that str also truncate after 10th decimal. str(1.123456789123456789) = '1.12345678912' Commented Dec 11, 2015 at 15:05

2 Answers 2

1

You cam simply use list comprehension:

>>> x = np.array([[1.5,1.3],[2.5,1.5]])
>>> y = [' '.join('{:.5e}'.format(col) for col in line) for line in x]
>>> y 
['1.50000e+00 1.30000e+00', '2.50000e+00 1.50000e+00']

You can change '{:.5}' to whatever format you want.

Sign up to request clarification or add additional context in comments.

7 Comments

Hello. This was nice:). But is not map() a function which is not very much Pythonic more? I have no idea but some forum mention that I should not use it anymore?
@fossekall This is probably opinion-based, but map (f, l) is simply syntaxic for (f(x) for x in l) in python 3.x.
@fossekall I have updated my answer to use a generator instead of map.
Is map a generator, since you use () instead of []?
@fossekall In python 2.x, map returns a list, in python 3.x map returns a generator. Since I am sending the result to join, using a generator is better here.
|
0

Here's one approach that starts off by converting the input numeric array to a string array with .astype('S4') conversion and then does one level of loop comprehension to join all strings in each row with " ".join(item), where item is each row from the string array, like so -

[" ".join(item) for item in x.astype('S4')]

If you are looking for precision control, you can change the dtype to S6, S12 or S14.

Sample run -

In [9]: x
Out[9]: 
array([[ 0.62047293,  0.02957529,  0.88920602,  0.57581068],
       [ 0.40903378,  0.80748886,  0.83018903,  0.22445871],
       [ 0.87296866,  0.94234112,  0.70001789,  0.99333763],
       [ 0.96689113,  0.35128491,  0.35775966,  0.26734985]])

In [10]: [" ".join(item) for item in x.astype('S4')]
Out[10]: 
['0.62 0.02 0.88 0.57',
 '0.40 0.80 0.83 0.22',
 '0.87 0.94 0.70 0.99',
 '0.96 0.35 0.35 0.26']

In [11]: [" ".join(item) for item in x.astype('S14')]
Out[11]: 
['0.620472934011 0.029575285327 0.889206021736 0.575810682998',
 '0.409033783485 0.807488858152 0.830189034897 0.224458707937',
 '0.872968659668 0.942341118836 0.700017893576 0.993337626766',
 '0.966891127767 0.351284905075 0.357759658063 0.267349854182']

Comments

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.