0

I have a string which is [-0.0597212 0.00344087 -0.23413301 0.02406648]

As you see, there is [ at first, there are 3 spaces between 1st and 2nd value, 1 space between 2nd and 3rd value, 2 spaces between 3rd and 4th value, and there is ] at the end.

I want to decompose these 4 numerical values with their signs and I will assign these 4 values to an 4-element array.

For example;

a[0]=-0.0597212
a[1]=0.00344087
a[2]=-0.23413301
a[3]=0.02406648

Normally there will be 128 elements with like this text (there will be maximum 3 spaces, minimum 1 space between values), but I want to try to find the solution with this example. I tried split.strip() and replace() functions but I didn't find any solution for me. Can you help me ?

1
  • 2
    Is there a reason why you're working with a string representation of a numpy array, rather than an actual numpy array? Commented Mar 10, 2019 at 10:34

3 Answers 3

2
a="[-0.0597212   0.00344087 -0.23413301  0.02406648]"

#split string
a = a.split() 

#delete open & close square bracket
a[0] =  a[0][1:] 
a[-1] = a[-1][:-1] 

#convert to float
for i in range(len(a)):
    a[i] = float(a[i])
Sign up to request clarification or add additional context in comments.

4 Comments

so much thanks, I tried with 128 elements and it worked !! but the only bug is, if it ends like "0.0192611 ]" (1 space before "]" ), it shows whole numbers but with an error "ValueError: could not convert string to float:", so do you have a solution for this?
Welcome dude :D For that case you can just add this code before convert to float: if a[-1]=='': a.pop(-1)
special thanks for answer dude, it works perfectly :) but I wondered the logic on a[0] and a[-1] line. Can you briefly explain this?
given variable a is list on python, a[0] mean the first value of list and a[-1] mean last value of the list, that's how Python work. a[0] = a[0][1:] changing the value of a[0] with substring of previous value from index 1 until last index (a[0] still a string and Python start index from 0). You can learn more (or maybe just find what you need) on this book: souravsengupta.com/cds2015/python/LPTHW.pdf
2
list(map(float, txt[1:-1].split()))

5 Comments

This just assumes that the first and last character should be removed, what if there is something around them, e.g. ` [ 1 2 3 ] `?
@zvone I made the liberty of making the assumption based on the question asked. It is very specifically mentioned about the first and last characters in the string.
thank you mujjiga but I didn't understand how can I print the whole elements of array?
You mean a = list(map(float, txt[1:-1].split())); print (a)
also if you have spaces around [ and ] you can use a = list(map(float, txt.strip()[1:-1].split()));print (a)
1

If you use numpy():

import numpy as np

s = '[-0.0597212   0.00344087 -0.23413301  0.02406648]'

np.fromstring(s.strip('[]'), dtype=float, sep=' ')
# [-0.0597212   0.00344087 -0.23413301  0.02406648]

You can also use ast.literal_eval:

import ast
import re

ast.literal_eval(re.sub(r'\s+', ',', s))
# [-0.0597212, 0.00344087, -0.23413301, 0.02406648]

1 Comment

thank you Mykola Zotko but I didn't understand how can I print the whole elements of array?

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.