Reputation: 1079
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
Upvotes: 1
Views: 841
Reputation: 12669
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]]
Upvotes: 2
Reputation: 1158
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)
Upvotes: 2
Reputation: 55
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)
Upvotes: 1
Reputation: 3547
[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]]
Upvotes: 2
Reputation: 2855
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'],
...
Upvotes: 1
Reputation: 164703
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)))
Upvotes: 1
Reputation: 1124
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])
Upvotes: 2