-3

I want to find out how to convert a list of strings into a list of numbers.

I have a php form through which user enters values for x and y like this:

X: [1,3,4]
Y: [2,4,5]

These values are stored into database as varchars. From there, these are called by a python program which is supposed to use them as numerical (numpy) arrays. However, these are called as plain strings, which means that calculation can not be performed over them. Is there a way to convert them into numerical arrays before processing or is there something else which is wrong?

Levon
  • 138,105
  • 33
  • 200
  • 191
khan
  • 7,005
  • 15
  • 48
  • 70
  • 1
    Parse the strings and split them to form the array. This is not difficult. [Try something](http://whathaveyoutried.com). –  Jun 11 '12 at 17:51

4 Answers4

2

You can use list comprehension along with the strip() and split() function to turn this into numeric values.

x = '[1,3,4]'
new_x = [int(i) for i in x.strip('[]').split(',')]
new_x
[1, 3, 4]

Use this list of ints as you see fit, e.g., passing them on to numpy etc.

from numpy import array

a = array(new_x)
a
array([1, 3, 4])

a * 4
array([ 4, 12, 16])
Levon
  • 138,105
  • 33
  • 200
  • 191
  • Thanks guys. But the thing is that this resulting array can not be multiplied or added with any other array. What I want this thing to do is being able to multiply and add like normal numerical arrays. :/ – khan Jun 11 '12 at 16:43
  • I will. I will post my complete snippet and posting code over here today.. let's see if this can be resolved. – khan Jun 11 '12 at 16:55
  • @khan my solution gives you numbers, so I am not quite sure what you are looking for. I'll take a look at your updated question. – Levon Jun 11 '12 at 16:57
  • Yeah..i am in my office and will update the question once I get to home. – khan Jun 11 '12 at 17:06
1

Here's one way:

>>> import numpy
>>> block = "[1,3,4]"
>>> block = block.strip("[]")
>>> a = numpy.fromstring(block, sep=",", dtype=int)
>>> a
array([1, 3, 4])
>>> a*2
array([2, 6, 8])
jterrace
  • 64,866
  • 22
  • 157
  • 202
  • It is still returned as a string. My point is that it should be returned as a numerical array. For instance: >>> a = numpy.array( [2,3,4] ) Now when we do a*2: >>> a*2 >>> array( [4,6,8] ) This is ideal. I want this to happen with my string but so far not able to do so. :/ – khan Jun 11 '12 at 16:50
  • What? That is not a string at all. It's a numpy array. I've updated the answer to maybe make it more clear what's going on. – jterrace Jun 11 '12 at 17:00
  • I am sorry..I meant to say that it is returned as an array of characters rather than an array of numerical entities which have the capability to add up or get multiplied with another array of numbers like matrices. – khan Jun 11 '12 at 17:02
  • ?? No it's not. Did you look at the answer? – jterrace Jun 11 '12 at 17:03
  • Let me get to home and post my entire code over here for you guys for better comprehension. – khan Jun 11 '12 at 17:09
  • this is another way of doing it..good..both you and levon are correct. Thanks :-) – khan Jun 12 '12 at 00:55
0
import ast
import numpy as np

def parse_array(s):
    return np.array(ast.literal_eval(s))

s = '[1,2,3]'
data = parse_array(s)    # -> numpy.array([1,2,3])
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0

If I understand your question correctly, you can use the eval() and compile() built-in functions to achieve your aim:

>>> lst_str = '[1,2,3]'
>>> lst_obj = compile(lst_str, '<string>', 'eval')
>>> eval(lst_obj)
[1, 2, 3]

Keep in mind that using eval() in this manner is potentially unsafe, however, unless you can validate the input.

Greg E.
  • 2,722
  • 1
  • 16
  • 22
  • @PauloScardine, I would imagine that it must be, but until I read Hugh Bothwell's post above, I didn't even know it existed. Still, `ast` apparently wasn't added until 2.6, so the `eval()` method might have some use in old versions. – Greg E. Jun 11 '12 at 17:22