1

I'm working in Python and currently I have a list which looks like

['001   2.4600       0.46  2.36E+003   86.66  16.77       0.33  1.32E+003   74.41  17.61       0.40  2.21E+003   87.39  22.07',
 '002   10.310       0.38  2.95E+002   76.88   4.53       0000  000000000   00000   0000       0.34  2.62E+002   97.36   4.41',
 '003   74.840       0.63  5.07E+002   64.63   4.03       0.57  4.15E+002   61.96   3.99       0.63  5.43E+002   64.67   5.16',
...

and so on, with quite a few more elements. Each element of the list is a string, containing various figures which have spaces between them. i.e, as above, the first element has 001, 2.4600, 0.46 and so on.

The point is that I want to turn each element of the list into a row of an array. The aim is to have a large array giving me all the information which is currently just numbers separated by spaces inside strings in a list.

I'm sure I can use the built in array module to do this but I just can't figure out how.

Any ideas? Hope the question is clear.

4 Answers 4

3

Assuming you want floats in the final list of lists, try this:

>>> data = ['001 2.4600 0.46 2.36E+003 86.66 16.77 0.33 1.32E+003 74.41 17.61 0.40 2.21E+003 87.39 22.07', '002 10.310 0.38 2.95E+002 76.88 4.53 0000 000000000 00000 0000 0.34 2.62E+002 97.36 4.41', '003 74.840 0.63 5.07E+002 64.63 4.03 0.57 4.15E+002 61.96 3.99 0.63 5.43E+002 64.67 5.16']

>>> [list(map(float, row.split())) for row in data]
[[1.0, 2.46, 0.46, 2360.0, 86.66, 16.77, 0.33, 1320.0, 74.41, 17.61, 0.4, 2210.0, 87.39, 22.07], [2.0, 10.31, 0.38, 295.0, 76.88, 4.53, 0.0, 0.0, 0.0, 0.0, 0.34, 262.0, 97.36, 4.41], [3.0, 74.84, 0.63, 507.0, 64.63, 4.03, 0.57, 415.0, 61.96, 3.99, 0.63, 543.0, 64.67, 5.16]]

map just says 'do this function (float()) on everything in this list (the result of split(), which is a list of strings)'. In Python 3 it returns an iterator, so we have to ask for the list() of it. It's often better to use a for loop or list comprehension instead of map, but in this case it's handy.

Sign up to request clarification or add additional context in comments.

Comments

2

Your idea of using the array module is probably bogus, as an array.array object is, essentially, a list with constrained data type. You cannot use vectorized operations on them. Further, an array.array is a 1D object.

That said, you possibly want to use the numpy module, whose array object is a multidimensional array on which you can operate at your will.

# idiomatic manner of importing numpy
import numpy as np

data = ['1 2 3.', '4. 5 8']
arraydata = np.array([[float(n) for n in row.split()] for row in data])
print arraydata

# [[ 1.  2.  3.]
#  [ 4.  5.  8.]]

2 Comments

This appears to have worked beautifully. The only slight quibble is that when I do the equivalent of "print arraydata" it prints 2 separate array (which I want) but both with 4 rows and 4 columns, rather than 1 row and 14 columns.
I guess you are mistaking the way the array is printed (with 4 numbers per row) with the internal representation of the data (which is organized in rows of 14 elements). You can check what I mean by printing individual entries of your array, like print arraydata[1,11] that gives you 260.0, i.e., the 12th element in the 2nd row (remember, Python counts starting from zero, and the 1st element while addressing a 2D array is the row count).
1

Hopefully I understood correctly

res = []
for row in my_list:
    res.append(list(map(float, row.split())))

Here you will have a matrix of values, in string format. Added conversion

2 Comments

I don't think split(' ') would work. Looks like OP's strings have either multiple spaces, or tabs instead of spaces. Instead, just use split() to split at any whitespace characters. Also, you could make this a list comprehension: res = [x.split() for x in lst]
Agree, I will modify it
0

Asuming your data is stored in a list called data you could use data =[[int(el) for el in string.split(' ')] for string in data]

1 Comment

Do those look like integers? Also, better use split() instead of split(' ')

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.