Home > Blockchain >  split input data and have one array output
split input data and have one array output

Time:12-12

I would like to split an input data to an numpy array but I got a separated 3 elements in array not as one array with all elements as the following:

import numpy as np
with open('dataa.dat', 'r') as f:
     input_data = f.readlines()
     input_data = [(d ' ')[:d.find('#')].rstrip() for d in input_data]

     t_f = input_data[0].split("-",2)
     t_f2 = []
     for elt in t_f:
         if "*" in elt:
             n, mult = elt.split("*")
             t_f2 = t_f2   [int(n)] * int(mult)
        else:
            t_f2.append(elt)
    c_w = np.array(t_f2)
    print('kk',c_w) # To be like this: [1 1 1 1 2 1 1 1 1]

CodePudding user response:

I think you forgot to split the numbers separated by the commas. You can do that by replacing the line

t_f2.append(elt)

with

t_f2  = [int(x) for x in elt.split(',')]
import numpy as np
with open('dataa.dat', 'r') as f:
    input_data = f.readlines()
    input_data = [(d ' ')[:d.find('#')].rstrip() for d in input_data]

    t_f = input_data[0].split("-",2)
    t_f2 = []
    for elt in t_f:
        if "*" in elt:
            n, mult = elt.split("*")
            t_f2 = t_f2   [int(n)] * int(mult)
        else:
            t_f2  = [int(x) for x in elt.split(',')]
    c_w = np.array(t_f2)
    print('kk',c_w) # To be like this: [1 1 1 1 2 1 1 1 1]
  • Related