Search code examples
pythonarrayslistzip

Using Python to zip and list functions to combine two 2D arrays to produce x,y coordinates?


I have two, 2D-arrays each of dimensions 831 x 918. If:

Matrix A =[[a(1,1), a(1,2),...],[a(2,1),a(2,2)...]] Matrix B =[[b(1,1), b(1,2),...],[b(2,1),b(2,2)...]]

I would like to combine the two matrices to create a list with ((a(1,1),b(1,1)),(a(1,2),b(1,2)),...).

What is the best way to do this? I have tried to use the zip and list functions as shown below:

import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt

ly=fits.open(lightyield.fits)
ly=ly[0].data
dx=fits.open(de.fits)
dx=dx[0].data
combo=list(zip(dx,ly))

However, this returns a list that is 831 elements long, but each position on the list has a lot of elements. I only want to have a single (x,y) point for each element in the list, not an entire array.

Thanks for any help. EDIT: As a basic example, for: A=[[1,2,3],[4,5,6],[7,8,9]] B=[[10,11,12],[13,14,15],[16,17,18]]

I want to get a list to plot of ((1,10),(2,11),(3,12),...).


Solution

  • If you have two arrays of the same shape like:

    a = np.random.random((10, 15))
    b = np.random.random((10, 15))
    

    You can perform this operation to get a list with the pairs of elements in the same position

    [(a_, b_) for a_, b_ in zip(a.flatten(), b.flatten())]
    

    Hope it helps