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.
>Solution :
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)]