Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenating numpy row elements

Tags:

python-3.x

If I have three numpy arrays:

a = np.array([[1, 2], [3, 4],[5,6]])
b = np.array([[7, 8], [9,10],[11,12]])
c = np.array([[13, 14], [15,16], [17,18]])
dstacker = np.dstack((a,b,c))

I want to save this to a file in the following format:

1713, 2814, 3915, 41016, 51117, 61218

Having tried many methods and having searched stack exchange exhaustively, Im currently stumped. What are my options?

like image 967
Dakota Avatar asked Dec 18 '25 05:12

Dakota


1 Answers

Maybe this isn't the most elegant solution, but I figured out my formatting problem by doing the following with help from a generous member here -- Donkey Kong:

new_dstack_list =[''.join([hex(ele) for ele in row]) for dim in dstacker for row in dim]  #supplied by Donkey Kong

number_of_elements = len(new_dstack_list)

for i in range(0,number_of_elements):
    new_dstack_list[i] = new_dstack_list[i].replace(' ', '').replace('0x', '')

saving the file:

with open(save_file_path,'w') as f:
    f.write(",".join(list))
    f.close()

and now the output is:

17d,28e,39f,4a10,5b11,6c12

Thanks again for the help, it gave me a push in the right direction!

like image 138
Dakota Avatar answered Dec 21 '25 02:12

Dakota



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!