0

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
4
  • self explained: You put 2 agrument in to write() while there should be one Commented Mar 7, 2022 at 5:30
  • Is there a way to put multiple arguments using write() or may be some other function? Commented Mar 7, 2022 at 5:38
  • just add all to a string. Commented Mar 7, 2022 at 5:39
  • also you have file2write=open("Data",'w') which will create a file call Data (no extension) Commented Mar 7, 2022 at 5:52

1 Answer 1

1

Write() only takes one argument. If you want to write it in a file you need to format the line. Something like:

file2write.write(str([timestamps])+str([T]))

https://www.w3schools.com/python/ref_file_write.asp

Sign up to request clarification or add additional context in comments.

4 Comments

Using this, I get the output in the following format: [[0.77619895, 0.479028, 0.07942242, 0.58715807]][[0.0, 0.0, 0.0, 0.0]]. I am seeking to get in two columns as I mentioned in the post.
File handling is for plaintext, you can not have columns or something like that, what you can do is format your write with \t to add a tab after your first variable. file2write.write(str([timestamps])+"\t"+str([T]))
Under what conditions can I have columns? Is there a different way to write for columns?
Using CSV format maybe is what you are looking for: pythontutorial.net/python-basics/python-write-csv-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.