Create an array from a txt file

I am new to python and I have a problem. I have some measured data stored in a txt file. data is separated by tabs, it has the following structure:

0   0   -11.007001  -14.222319  2.336769

i always have 32 datasets for simulation (0,1,2, ..., 31), and I have 300 simulations (0,1,2 ..., 299), so the data is first sorted using the simulation number, and then the data point number.

The first column is the model number, the second column is the data point number, and the remaining 3 columns are the x, y, z coordinates.

I would like to create a 3d array, the first dimension should be the number of simulations, the second should be the number of data and the third should be three coordinates.

I already started a bit, and here is what I still have:

## read file
coords = [x.split('\t') for x in
          open(f,'r').read().replace('\r','')[:-1].split('\n')]
## extract the information you want
simnum = [int(x[0]) for x in coords]
npts = [int(x[1]) for x in coords]
xyz = array([map(float,x[2:]) for x in coords])

but I don’t know how to combine these 2 lists and this one array.

in the end I would like to have something like this:

array = [simnum] [num_dat_point] [xyz]

.

, , python, , - , .

+3
7

zip function, :

for sim, datapoint, x, y, z in zip(simnum, npts, *xyz):
    # do your thing

:

for line in open(fname):
    lst = line.split('\t')
    sim, datapoint = int(lst[0]), int(lst[1])
    x, y, z = [float(i) for i in lst[2:]]
    # do your thing

, ( ) :

coords = [x.split('\t') for x in open(fname)]
+2

zen python, , . dict.

import csv
f = csv.reader(open('thefile.csv'), delimiter='\t',
               quoting=csv.QUOTE_NONNUMERIC)

result = {}
for simn, dpoint, c1, c2, c3 in f:
    result[simn, dpoint] = c1, c2, c3

# pretty-prints the result:
from pprint import pprint
pprint(result)
+2

itertools.groupby.

import itertools
import csv
file = open("data.txt")
reader = csv.reader(file, delimiter='\t')
result = []
for simnumberStr, rows in itertools.groupby(reader, key=lambda t: t[0]):
    simData = []
    for row in rows:
        simData.append([float(v) for v in row[2:]])
    result.append(simData)
file.close()

"". , - . , x, y z.

, , .

+2

, , .

. t max(simnum) x max(npts) x 3. , -, .

-

for x in coords:
  t[int(x[0])][int(x[1])][0]=float(x[3])
  t[int(x[0])][int(x[1])][1]=float(x[4])
  t[int(x[0])][int(x[1])][2]=float(x[5])

, ?

+1

, array . Python array, , array.array ( 1-D ); , numpy, numpy.array, , , , numpy, , ; list dict. , - , , , (, , , , DO , "", , ..).

csv , . , coords 5 , , ints, . ...

, , - , - ? , , - , ? , , , , ; ( / ).

: (0, 1, 2,...), ( btw, 0, 1, 2,...)? , - , , . , : , - , ? , , , .

dict , : , , , . ( ), , 3 .

...:

def make_container(coords):
  result = dict()
  for s, d, x, y, z in coords:
    key = int(s), int(d)
    value = float(x), float(y), float(z)
    result[key] = value
  return result

def (.. , , ), . make_container , ; ,

d = make_container(coords)
print d[0, 0]

x, y, z dp 0 sim 0, ( , sim/dp ). dicts , .

print d.get((0, 0))

(, do - , , get ), None, , sim/dp (0, 0).

, (, , , , ), , ( - , !), - ! -)

+1

, , , , : -)

def parse(line):
    mch = re.compile('^(\d+)\s+(\d+)\s+([-\d\.]+)\s+([-\d\.]+)\s+([-\d\.]+)$')
    m = mch.match(line)
    if m:
        l = m.groups()
        (idx,data,xyz) = (int(l[0]),int(l[1]), map(float, l[2:]))
        return (idx, data, xyz)
    return None

finaldata = []
file = open("data.txt",'r')
for line in file:
    r = parse(line)
    if r is not None:
        finaldata.append(r)

:

[(0, 0, [-11.007001000000001, -14.222319000000001, 2.3367689999999999]),
 (1, 0, [-11.007001000000001, -14.222319000000001, 2.3367689999999999]),
 (2, 0, [-11.007001000000001, -14.222319000000001, 2.3367689999999999]),
 (3, 0, [-11.007001000000001, -14.222319000000001, 2.3367689999999999]),
 (4, 0, [-11.007001000000001, -14.222319000000001, 2.3367689999999999])]

( - - )...

, , , ... python 2.6.

0

, 3D- - , ? , 2- , - , - , , , .

.

data = []
for coord in coords:
    if coord[0] not in data:
        data[coord[0]] = []
    data[coord[0]][coord[1]] = (coord[2], coord[3], coord[4])

7, 13, [7] [13]

0

Source: https://habr.com/ru/post/1714797/


All Articles