I am trying to save the lists timestamps and T to a file in two columns. The error and the desired output is attached.
import numpy as np
import time
def get_neighbor_indices(position, dimensions):
'''
dimensions is a shape of np.array
'''
i, j = position
indices = [(i+1,j), (i-1,j), (i,j+1), (i,j-1)]
return [
(i,j) for i,j in indices
if i>=0 and i<dimensions[0]
and j>=0 and j<dimensions[1]
]
def iterate_array(init_i, init_j, arr, condition_func):
'''
arr is an instance of np.array
condition_func is a function (value) => boolean
'''
indices_to_check = [(init_i,init_j)]
checked_indices = set()
result = []
t0 = None
t1 = None
timestamps = []
while indices_to_check:
pos = indices_to_check.pop()
if pos in checked_indices:
continue
item = arr[pos]
checked_indices.add(pos)
if condition_func(item):
result.append(item)
t1=time.time()
if(t0==None):
t0=t1
timestamps.append(t1-t0)
indices_to_check.extend(
get_neighbor_indices(pos, arr.shape)
)
return result,timestamps
P1=np.array([[0.77619895, 0.479028 , 0.07942242, 2.07133691, 7.60581783],
[2.13382399, 0.58715807, 2.00747824, 0.48842203, 1.17064478],
[1.96443702, 4.19886806, 0.68915071, 0.17770271, 0.47118552],
[0.87647195, 0.18112885, 0.30398434, 0.46239019, 1.17481443],
[2.80282535, 0.53706217, 1.21285428, 2.83677513, 3.04105655]])
file2write=open("Data",'w')
timestamps,T=iterate_array(0,0, P1, lambda x : x < 2)
print(timestamps)
print(T)
file2write.write([timestamps],[T])
file2write.close()
Error is
file2write.write([T],[timestamps])
TypeError: TextIOWrapper.write() takes exactly one argument (2 given)
The desired output includes storing two arrays to a file in the following format:
(timestamps) (T)
0.0 0.77619895
0.0 0.479028
0.0 0.07942242
0.0 0.58715807
write()while there should be onewrite()or may be some other function?file2write=open("Data",'w')which will create a file callData(no extension)