[Tutor] create numpy array from list of strings
Danny Yoo
dyoo at cs.wpi.edu
Wed Jun 4 00:04:22 CEST 2008
> Hello, I have a list of strings:
> data =
> [ '33386.47 2.22 -1.11 0.43'
> '33386.67 3.33 -1.23 0.54'
> ...
> '46728.47 0.1 -1.87 -0.54'
> '46728.47 9.7 5.68 0.38'
> '46729.47 -0.17 -0.47 0.23']
>
> I wish to convert it to a numpy array of floats. How do I accomplish
> this?
Pointers:
* Give a name to the thing you're trying to define. It sounds like
you're trying to define a function that takes a list of strings and
converts it to a numpy array. Give it a good, concise name. For
example, "convertStringsToNumpyArray" might be a bit wordy, but it
works.
* Write out your expectations on what this function will do on small
examples. That way, once you have the function written, you can test
it. For example:
convertStringsToNumpyArray['42'] ==> the singleton array containing 42
convertStringToNumpyArray['3 1 4'] ==> the row array containing 3, 1,
and 4.
Cook up a few more examples for yourself.
* Try to simplify the problem slightly. If you had a list of the number
rows, would you be able to turn that into a numpy array? i.e. if you
were given:
data = [[1, 2], ## row 1
[3, 4], ## row 2
]
could you write a function that consumes that data and converts it into
a numpy array?
More information about the Tutor
mailing list