Home > Back-end >  combining array and list elemint by index
combining array and list elemint by index

Time:10-19

I have an array (which comes from a kdtree):

array =  [[a b c d e]
          [a b c d e]
          [a b c d e]]

and a list :

lst = [1, 2, 3, 4, 5]

I want to do some list comprehension (using array and lst) that makes it look like this:

desired_result = [[a, b, c, d, e, 1]
                  [a, b, c, d, e, 2]
                  [a, b, c, d, e, 3]]

I am familiar with list comprehension just not familiar enough to know how to deal with this.

CodePudding user response:

If you want a list comprehension:

result = [l [x] for l,x in zip(array, lst)]

CodePudding user response:

An alternative way to list comprehension if array is of the type numpy.ndarray:

result = numpy.c_[array, lst]

Please check this answer for more details.

  • Related