1

I have the following list

['0, 0, 0.16548735788092,\n',
 '1, 3.90625E-05, 0.368149412097409,\n',
 '2, 7.8125E-05, 0.184674297925085,\n',
 '3, 0.0001171875, 0.00359755125828087,\n',
 '4, 0.00015625, 0.0131910212803632,\n',
 '5, 0.0001953125, 0.24703185306862,\n',
 '6, 0.000234375, 0.474876766093075,\n',]

BWhich I obtained from

data = f.readlines()

I would like to transform this list into an array with 3 columns. Thanks

2
  • you need show what you have try. Commented Feb 2, 2018 at 14:34
  • You can get the first element of that array, split by \n and then split by ,. Good luck! Commented Feb 2, 2018 at 14:34

7 Answers 7

2

We iterate per line. Then for each line we split on the , and assign the respective values to temporary values and then add a column with three entries to the array

array=[]
for line in f.readlines():
    a_0,a_1,a_2,_=line.split(",")
    array.append([a_0,a_1,a_2])
Sign up to request clarification or add additional context in comments.

Comments

2
[list(map(float, i.strip().split(',')[:-1])) for i in mylist]

Output:

[[0.0, 0.0, 0.16548735788092],
 [1.0, 3.90625e-05, 0.368149412097409],
 [2.0, 7.8125e-05, 0.184674297925085],
 [3.0, 0.0001171875, 0.00359755125828087],
 [4.0, 0.00015625, 0.0131910212803632],
 [5.0, 0.0001953125, 0.24703185306862],
 [6.0, 0.000234375, 0.474876766093075]]

Comments

2

If you want your list as a number, here you go:

list_3d = []

for line in data:
    columns = list(map(float, line.replace(' ','').split(',')[:-1]))
    list_3d.append(columns)

print(list_3d)

Comments

2

You can try something like this:

data=['0, 0, 0.16548735788092,\n',
 '1, 3.90625E-05, 0.368149412097409,\n',
 '2, 7.8125E-05, 0.184674297925085,\n',
 '3, 0.0001171875, 0.00359755125828087,\n',
 '4, 0.00015625, 0.0131910212803632,\n',
 '5, 0.0001953125, 0.24703185306862,\n',
 '6, 0.000234375, 0.474876766093075,\n',]



print(list(map(lambda x:list(map(float,x.split(',')[:-1])),data)))

output:

[[0.0, 0.0, 0.16548735788092], [1.0, 3.90625e-05, 0.368149412097409], [2.0, 7.8125e-05, 0.184674297925085], [3.0, 0.0001171875, 0.00359755125828087], [4.0, 0.00015625, 0.0131910212803632], [5.0, 0.0001953125, 0.24703185306862], [6.0, 0.000234375, 0.474876766093075]]

Comments

1

This is one way. Input list is lst. The output is a list of 3 tuples (one for each column). I've also converted the numbers into float and removed \n.

cols = list(zip(*(map(float, i.replace('\n', '').split(',')[:-1]) for i in lst)))

Comments

1

You can use string.split, using ', ' as the delimiter.

new_list = []

for line in data:
    line = line.strip()      # get rid of \n
    line = line.strip(',')   # get rid of trailing comma
    line = line.split(', ')  # split into items
    new_list.append(line)    # add to the new list

The output is then:

[['0', '0', '0.16548735788092'],
 ['1', '3.90625E-05', '0.368149412097409'],
 ...

Comments

1

As I Understand from the question , there is need of 2D of (mx3) array, that stores each element of the data variable.

#creating 2-D Matrix 
Matrix = [[0 for x in range(0,3)] for y in range(0,len(data))]
    for x in data :
        a1=x.split(",") #split each element
        for i in range(0,len(data)):
            for j in range(0,3): #removes inserting \n
                  Matrix[i][j]=a1[j] 
print(Matrix)

Comments

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.