0

This is a list in which every line contains one string. For example '1, -40., -3.' is one string. i want to firstly divide the list to columns with numpy and then convert the strings to floats or vice versa. Is that possible? I tried removing the spaces but with no luck. The end result will be a matrix with 3 columns and x lines in which all elements are floats. Is something like that possible?

  1,         -40.,          -3.
  2,         -40.,          -6.
  3,         -40.,          -9.
  4,         -40.,         -12.
  5,         -40.,         -15.
  6,         -40.,         -18.
  7,         -40.,         -21.
  8,         -40.,         -24.
  9,         -40.,         -27.
 10,         -40.,           0.
 11,         -40.,         -30.
 12,         -36.,         -30.
 13,         -34.,         -30.
 14,         -30.,         -30.
 15,         -28.,         -30.
 16,         -24.,         -30.
 17,         -22.,         -30.
 18,         -18.,         -30.
 19,         -16.,         -30.
 20,         -12.,         -30.
 21,         -10.,         -30.
 22,          -6.,         -30.
 23,          -4.,         -30.
 24,           0.,         -30.
 25,           4.,         -30.
 26,           6.,         -30.
 27,          10.,         -30.
 28,          12.,         -30.
 29,          16.,         -30.
 30,          18.,         -30.
 31,          22.,         -30.
 32,          24.,         -30.
 33,          28.,         -30.
 34,          30.,         -30.
 35,          34.,         -30.
 36,          36.,         -30.
 37,          40.,         -30.
 38,          40.,         -27.
 39,          40.,         -24.
 40,          40.,         -21.
 41,          40.,         -18.
 42,          40.,         -15.
 43,          40.,         -12.
 44,          40.,          -9.
 45,          40.,          -6.
 46,          40.,          -3.
 47,          40.,           0.
 48,  -37.0502472,  -3.57497835
 49,  -34.6275177,  -6.06847525
 50,  -31.1389275,  -9.11990738
 51,  -27.3827724,  -11.8351984
 52,  -24.4094028,  -13.6369238
 53,  -20.2638969,  -15.7096758
 54,   -17.038496,  -17.0073357
 55,  -12.6125698,  -18.3830872
 56,    -8.078269,  -19.3431015
 57,  -4.62983418,  -19.7851849
 58,           0.,         -20.
 59,   4.62983418,  -19.7851849
 60,     8.078269,  -19.3431015
 61,   12.6125698,  -18.3830872
2
  • Where are you getting this data from? What is the source? It appears to be a csv. Commented Jul 4, 2017 at 14:13
  • it is a .txt input file Commented Jul 4, 2017 at 14:21

1 Answer 1

2

you can use:

x = map(lambda x: float(x), str.split(<line>, ',\t')

To describe this:

  1. split the line of string by tab (or whatever)
  2. feed the subsequent array into a map function, which applies the function in the first argument to every item in the second
  3. the first argument to the map function is a lambda function which simply takes a string in and returns a float
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.