Add a value to a list of paired values

Question:

I have an array that has pairs of numbers representing row, col values in a model domain. I am trying to add the layer value to have a list of lay, row, col.

I have an array rowcol:

array([(25, 65), (25, 66), (25, 67), (25, 68), (26, 65), (26, 66),
       (26, 67), (26, 68), (26, 69), (27, 66), (27, 67), (27, 68),
       (27, 69), (28, 67), (28, 68)], dtype=object)

and I want to add an 8 to each pair so it looks like

array([(8, 25, 65), (8, 25, 66), (8, 25, 67), (8, 25, 68), (8, 26, 65), (8, 26, 66),
       (8, 26, 67), (8, 26, 68), (8. 26, 69), (8, 27, 66), (8, 27, 67), (8, 27, 68),
       (8, 27, 69), (8, 28, 67), (8, 28, 68)], dtype=object)

I created a new array (layer) that was the same length as rowcol and zipped the 2 with:

layrowcol = list(zip(layer, rowcol))

and ended up with:

[(8, (25, 65)), (8, (25, 66)), (8, (25, 67)), (8, (25, 68)), (8, (26, 65)), (8, (26, 66)), (8, (26, 67)), (8, (26, 68)), (8, (26, 69)), (8, (27, 66)), (8, (27, 67)), (8, (27, 68)), (8, (27, 69)), (8, (28, 67)), (8, (28, 68))]

So it sort of worked and yet didn’t quite. Is there a way to combine them and leave out the unwanted parentheses or some better way to add the layer value to each pair without using zip(). Any help is appreciated.

Asked By: jgomn

||

Answers:

You can try this.

* operator infront of a tuple unpacks it to its constituents.
I am using list comprehension to iterate over each element of input array and create a new tuple out of it with 8 in the beginning.

arr = np.array([(25, 65), (25, 66), (25, 67), (25, 68), (26, 65), (26, 66),
       (26, 67), (26, 68), (26, 69), (27, 66), (27, 67), (27, 68),
       (27, 69), (28, 67), (28, 68)], dtype=object)
out = np.array([(8,*x) for x in arr])
out = list(map(tuple,out))

Output:

[(8, 25, 65), (8, 25, 66), (8, 25, 67), (8, 25, 68), (8, 26, 65), (8, 26, 66),
       (8, 26, 67), (8, 26, 68), (8. 26, 69), (8, 27, 66), (8, 27, 67), (8, 27, 68),
       (8, 27, 69), (8, 28, 67), (8, 28, 68)]
Answered By: MSS

You can use numpy.insert.

>>> import numpy as np
>>> a = np.array([(25, 65), (25, 66), (25, 67), (25, 68), (26, 65), (26, 66),(26, 67), (26, 68), (26, 69), (27, 66), (27, 67), (27, 68),(27, 69), (28, 67), (28, 68)], dtype=object)
>>> b = np.insert(a, 0, 8, axis=1)

Output:

array([[8, 25, 65],
       [8, 25, 66],
       [8, 25, 67],
       [8, 25, 68],
       [8, 26, 65],
       [8, 26, 66],
       [8, 26, 67],
       [8, 26, 68],
       [8, 26, 69],
       [8, 27, 66],
       [8, 27, 67],
       [8, 27, 68],
       [8, 27, 69],
       [8, 28, 67],
       [8, 28, 68]], dtype=object)

If you want back to the list of tuples.

>>> list(map(tuple, b))
[(8, 25, 65),
 (8, 25, 66),
 (8, 25, 67),
 (8, 25, 68),
 (8, 26, 65),
 (8, 26, 66),
 (8, 26, 67),
 (8, 26, 68),
 (8, 26, 69),
 (8, 27, 66),
 (8, 27, 67),
 (8, 27, 68),
 (8, 27, 69),
 (8, 28, 67),
 (8, 28, 68)]
Answered By: I'mahdi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.