Home > OS >  how to merge list of arrays and each element in one array into list of tuples in numpy
how to merge list of arrays and each element in one array into list of tuples in numpy

Time:10-08

So in python, I have list of arrays that looks like

    [array([628,  688,  924, 1598], dtype=int32), 
    array([ 957, 1983, 2031, 2429], dtype=int32),
    array([1243, 1598, 2872], dtype=int32)]

and an array that looks like

    array([1, 2, 3], dtype = int32])

I want to merge the 2 into list of tuples, or something like this.

    [(array([628,  688,  924, 1598], dtype=int32), 1), 
    (array([ 957, 1983, 2031, 2429], dtype=int32), 2),
    (array([1243, 1598, 2872], dtype=int32), 3)]

Would anyone help me on coding this?

Thanks!

CodePudding user response:

You can use zip():

a = [array([628,  688,  924, 1598], dtype=int32), 
    array([ 957, 1983, 2031, 2429], dtype=int32),
    array([1243, 1598, 2872], dtype=int32)]
b = array([1, 2, 3], dtype = int32)          # there was an extra ] here
c = list(zip(a, b))

Result:

[(array([ 628,  688,  924, 1598], dtype=int32), 1),
 (array([ 957, 1983, 2031, 2429], dtype=int32), 2),
 (array([1243, 1598, 2872], dtype=int32), 3)]
  • Related