1

I'm very new to python. I wondered how I can concatenate array a and b in this particular way.

I tried using stack, vstack, hstack, concatenate and a whole lot more, but I'm still not able to get my desired result.

a=  [[   0],
     [   1],
     [   2],
     [   3],
     [   4],
     [   5],
     [   6]]

b=  [[1,2,3],
     [1,2,3],
     [1,2,3],
     [1,2,3],
     [1,2,3],
     [1,2,3],
     [1,2,3]]

c = np.somefunction(a,b)


c = [
    [[0],[1,2,3]],
    [[1],[1,2,3]],
    [[2],[1,2,3]],
    [[3],[1,2,3]],
    [[4],[1,2,3]],
    [[5],[1,2,3]],
    [[6],[1,2,3]],
    ]
DJosh
  • 15
  • 1
  • 5
  • Shouldn't `c` start from `[[0],[1,2,3]]`? – DirtyBit Jan 09 '19 at 11:45
  • is `[0]` really an element in `a`? because that means the merge would throw it away – Aemyl Jan 09 '19 at 11:45
  • I edited the array – DJosh Jan 09 '19 at 12:08
  • @DJosh well, now you don't need slicing, I edited my answer according to your edited question. cheers – DirtyBit Jan 09 '19 at 12:10
  • Did you learn anything from all those things that you tried? Most raise a mismatch shape error. That's an important piece of information. `hstack` runs but produces a (7,4) integer array. But your desired `c` is what? (7,2)? but what dtype? – hpaulj Jan 09 '19 at 17:26

4 Answers4

2

The problem is you have a jagged array. In other words, each sublist in your list of lists does not have the same length, so you cannot construct a regular NumPy int array.

Thus you can use a simple list comprehension or map. I assume that your a list of lists begins with [0] and ends with [5]:

c = [[i, j] for i, j in zip(a, b)]  # list comprehension
c = list(map(list, zip(a, b)))      # functional version

# [[[0], [1, 2, 3]],
#  [[1], [1, 2, 3]],
#  [[2], [1, 2, 3]],
#  [[3], [1, 2, 3]],
#  [[4], [1, 2, 3]],
#  [[5], [1, 2, 3]]]

Converting this to an array is possible, but it will have object dtype. For most purposes, it will have little benefit over a regular Python list of lists:

c_arr = np.array(c)

# array([[[0], [1, 2, 3]],
#        [[1], [1, 2, 3]],
#        [[2], [1, 2, 3]],
#        [[3], [1, 2, 3]],
#        [[4], [1, 2, 3]],
#        [[5], [1, 2, 3]]], dtype=object)
jpp
  • 159,742
  • 34
  • 281
  • 339
2

You can use list comprehensions. Using concatenate from NumPy won't work here because of the mismatch in the dimensions of the two arrays. Here I am assuming you want to start the array a from 1. If not, then you can simply replace a[1:] by a.

c = [list(i) for i in zip(a[1:],b)]

[[[1], [1, 2, 3]],
 [[2], [1, 2, 3]],
 [[3], [1, 2, 3]],
 [[4], [1, 2, 3]],
 [[5], [1, 2, 3]],
 [[6], [1, 2, 3]]]
Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

aaaand boom:

c = list(zip(a,b))
print([list(x) for x in c])

pyFiddle

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

your arrays look like lists in which case the following may help

c=[list(i) for i in zip(a,b)]

else if they are numpy arrays, this link may help you.

gireesh4manu
  • 109
  • 2
  • 12
  • Nope, this gives... `[([0], [1, 2, 3]), ([1], [1, 2, 3]), ([2], [1, 2, 3]), ([3], [1, 2, 3]), ([4], [1, 2, 3]), ([5], [1, 2, 3])]` – jpp Jan 09 '19 at 11:53
  • 1
    That's not the issue. Notice the extra parentheses. Your code gives a list of tuples, which isn't what OP wants. – jpp Jan 09 '19 at 11:54
  • I've modified my solution as per the correct output format. Thanks for pointing out the tuples in my initial version. – gireesh4manu Jan 09 '19 at 11:57