'How can I adding commas between the numbers in array?

import numpy
import re
data1 = []

with open("C:/Users/PycharmProjects/firsttry.txt", 'r') as textfiles:
for line in textfiles:
    data2 = [item.strip() for item in line.split(',')]
    data1.append(data2)
print(data1)

The result becomes

[['0.1 0.4 0.7'], ['0.2 0.5 0.8'], ['0.3 0.6 0.9']]

How can I change it into matrix form so that I can select number such as data[0][0] = 0.1?



Solution 1:[1]

I don't know what your input text file is - if I did, I could reduce it to one pass over the input lines. Here's another pass to get your result in the format you want:

newresult = []
for row in data1:
    newresult.append([float(c) for c in row[0].split(' ')])
print(newresult)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 luthervespers