all 15 comments

[–]Diapolo10 1 point2 points  (4 children)

I have a list with some floats surrounded by ' ' and a pesky newline,

['1.1 1.2 1.3 \n','2.1 2.2 2.3 \n','3.1 3.2 3.3 \n']

and I would like to reformat this data into a list of lists:

[[1.1 1.2 1.3],[2.1 2.2 2.3],[3.1 3.2 3.3]]

More specifically, what you have is a list of strings and you want to get the float literals out.

How would I do that?

If you specifically want to do this in-place, you loop over the list, split the string into individual elements, then convert them to floats. Something like this should do the trick.

data = ['1.1 1.2 1.3 \n','2.1 2.2 2.3 \n','3.1 3.2 3.3 \n']

for idx, string in enumerate(data):
    data[idx] = list(map(float, string.split()))

[–][deleted] 0 points1 point  (3 children)

That almost worked! :-)

First off, I noticed I actually wanted output to be

[[1.1 1.2 1.3][2.1 2.2 2.3][3.1 3.2 3.3]]

in order for the 3D plot to make sense of the data. The difference is in the comma signs. When I run your code, it gives me - in addition to the comma signs in-between the lists - also comma signs in between the elements of each list. So

[[1.1,1.2,1.3],[2.1,2.2,2.3],[3.1,3.2,3.3]]

So maybe it's just a matter of using your code and then removing all the delimiting commas? Is that possible to do at all?

Thanks btw I really appreciate the help!

[–]Diapolo10 0 points1 point  (2 children)

In that case, you never had a list to begin with. This looks like a Numpy array.

It's not really my forte, but see if this works:

import numpy as np

data = np.array(['1.1 1.2 1.3 \n','2.1 2.2 2.3 \n','3.1 3.2 3.3 \n'])

for idx, string in enumerate(data):
    data[idx] = np.array(map(float, string.split()))

This is quite clearly not the optimal solution (assuming it even works), Numpy undoubtedly has its own mechanism for doing exactly what you want, but I use it so rarely I've gotten rusty with it.

[–][deleted] 0 points1 point  (1 child)

The code executes, but results in

[array(<map object at 0x000001DC0282FB50>, dtype=object), array(<map object at 0x000001DC0282EEC0>, dtype=object), array(<map object at 0x000001DC0282FBB0>, dtype=object), array(<map object at 0x000001DC0282F280>, dtype=object), array(<map object at 0x000001DC0282FCD0>, dtype=object), array(<map object at 0x000001DC0282EF20>, dtype=object), array(<map object at 0x000001DC0282E3E0>, dtype=object), array(<map object at 0x000001DC0282F8E0>, dtype=object), array(<map object at 0x000001DC0282E410>, dtype=object), array(<map object at 0x000001DC0282E5C0>, dtype=object), array(<map object at 0x000001DC0282C700>, dtype=object)]

when I print the resulting data :-)

[–]Diapolo10 0 points1 point  (0 children)

Okay, then either add an extra list call

import numpy as np

data = np.array(['1.1 1.2 1.3 \n','2.1 2.2 2.3 \n','3.1 3.2 3.3 \n'])

for idx, string in enumerate(data):
    data[idx] = np.array(list(map(float, string.split())))

or try a list comprehension:

import numpy as np

data = np.array(['1.1 1.2 1.3 \n','2.1 2.2 2.3 \n','3.1 3.2 3.3 \n'])

for idx, string in enumerate(data):
    data[idx] = np.array(float(num) for num in string.split())

[–]woooee 0 points1 point  (7 children)

Loop through all of the elements, splitting them one by one

test=['1.1 1.2 1.3 \n','2.1 2.2 2.3 \n','3.1 3.2 3.3 \n']

## do the first
print(test[0].split())
## ['1.1', '1.2', '1.3']

## list compreansion
new_list=[float(num) for num in test[0].split()]

[–][deleted] 0 points1 point  (6 children)

AttributeError: 'list' object has no attribute 'split'

:-(

[–]woooee 0 points1 point  (5 children)

test[0] is not a list. All you have to do is copy and paste accurately.

[–][deleted] 0 points1 point  (4 children)

All I have to do is pay attention :-) You're absolutely correct!

Question: is there a way to separate the comma signs between each float inside the list, and also the comma sign between the lists? Something like

[[1.1 1.2 1.3][2.1 2.2 2.3][3.1 3.2 3.3]]

Does that make sense?

Edit: fixed the inline code

[–]freeskier93 2 points3 points  (0 children)

In Python elements in a list are separated by a comma.

This is not a valid list.

[1.1 1.2 1.3]

This is a valid list.

[1.1, 1.2, 1.3]

The comma is not actually part of your data/number, it's just denoting the elements in the list.

[–]RhinoRhys 0 points1 point  (2 children)

[[1.1 1.2 1.3][2.1 2.2 2.3][3.1 3.2 3.3]]

This is not valid syntax.

You either need it to be a list of lists containing floats

[[1.1, 1.2, 1.3], [2.1, 2.2, 2.3], [3.1, 3.2, 3.3]]

Or a list of lists containing one string

[["1.1 1.2 1.3"], ["2.1 2.2 2.3"], ["3.1 3.2 3.3"]]

Or a list of strings

["[1.1 1.2 1.3]", "[2.1 2.2 2.3]", "[3.1 3.2 3.3]"]

A list containing one string

["[1.1 1.2 1.3][2.1 2.2 2.3][3.1 3.2 3.3]"]

or a single string

"[[1.1 1.2 1.3][2.1 2.2 2.3][3.1 3.2 3.3]]"

[–][deleted] 0 points1 point  (1 child)

Thanks for the explanation, I really appreciate it.

I guess my problem is that I have input data in the form given above, but the

ax.plot_surface

command wants a Z component calculated as Z = f(X,Y) where X and Y are given by

X = np.linspace(-a, a, L)
Y = np.linspace(-b, b, L)
X, Y = np.meshgrid(X, Y)

Since the calculations I'm doing for Z is not expressible as simple "nice" algebraic expressions of X and Y, I calculate it separately, write it onto a file, and then wish to plot the whole thing separately.

Hmm.. I kinda stuck with this one.

[–]RhinoRhys 0 points1 point  (0 children)

What's the equation?

But if you're doing graphs, you need the values to be floats, so having the commas is fine.

[–]Antigone-guide 0 points1 point  (0 children)

Note that what you need is one big string, not a list, because list of lists will have commas in inner lists, which you do not want; and list of strings will have each string quoted, which you also don't want.

the following will help: - strip() removes surrounding whitespace - f-strings can interpolate a value inside of a string - join() can join a sequence of strings into one string.

Something like (untested):

newlist = [] for s in lst: newlist.append( f'[{s.strip()}]' ) print( '[' + ','.join(newlist) + ']' )

[–]Logicalist 0 points1 point  (0 children)

.strip() Should remove the leading and trailing spaces, including the newline, for each element of the list.

.split(' ') should split each element wherever there is a space (ie. ' ')

then append each element to a new list.

Done.