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1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0]
 [0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0]
 [0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0]
 [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0]]

I like to loop through the array. Everywhere where the array has a value of 1 I like to get the index of the array and perform a operation.

Roughly something like this:

for value in array:
   if value ==1:
     print arrayIndexX, arrayIndexY
3
  • What's the operation? What are you trying to do? From your other questions it looks like you are using NumPy. You probably want to read : scipy-lectures.github.io/intro/numpy/…
    YXD
    –  YXD
    2014-02-03 17:51:01 +00:00
    Commented Feb 3, 2014 at 17:51
  • Yeah, I am using numpy. I converted a GIS raster to a numpy array and like to get the coordinates.
    ustroetz
    –  ustroetz
    2014-02-03 17:52:08 +00:00
    Commented Feb 3, 2014 at 17:52
  • 1
    @ustroetz sure but what do you want to do with the coordinates? Do you want to call a function on another array of the same shape based on these coordinates? Do you want to use the indices to generate a polygon of the outline? Sometimes these questions are a case of meta.stackexchange.com/a/66378/160939 and you can sometimes get a much more useful answer with some broader details :)
    YXD
    –  YXD
    2014-02-03 17:56:22 +00:00
    Commented Feb 3, 2014 at 17:56

8 Answers 8

6

You can use numpy.where with numpy.column_stack here. Example:

>>> import numpy as np
>>> a = np.array([[1, 0, 0], [0, 1, 0], [1, 1, 1]])
>>> np.column_stack(np.where(a==1))
array([[0, 0],
       [1, 1],
       [2, 0],
       [2, 1],
       [2, 2]])
Sign up to request clarification or add additional context in comments.

Comments

5

Using enumerate, and a nested loop through rows and columns:

for y, row in enumerate(array):
    for x, val in enumerate(row):
        if val == 1:
            print x, y

Comments

2

In this case you could also use the np.nonzero( YourArray ) function, this will give you exactly what you want.

2 Comments

Apologies - I missed your answer before giving mine.
That's allright, I think it is important that this function popped up here somewhere, as is is most probably the fastest one.
1
for r, row in enumerate(array):
    for c, val in enumerate(row):
        if val == 1:
            print r,c

Alternatively, you could build a list containing the required coordinate values:

[(r,c) for r,row in enumerate(array) for c,val in enumerate(row) if val==1]

Comments

1

But, I think maybe far better is to use masked arrays...

marray = np.ma.array(b, mask=(b == 0))
print(marray)
[[-- -- -- -- -- 1 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --]
 [-- -- -- -- -- 1 1 1 1 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --]
 [-- -- -- -- -- 1 1 1 1 1 1 1 1 1 -- -- -- -- -- -- -- -- -- -- -- -- -- --]
 [-- -- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- -- -- -- -- -- -- -- -- --]
 [-- -- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- -- -- -- -- -- --]
 [-- -- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- -- -- --]
 [-- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- 1]
 [-- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- 1]
 [-- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- 1]
 [-- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- 1]
 [-- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- --]
 [-- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- --]
 [-- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- --]
 [-- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- --]
 [-- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- --]
 [-- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- --]
 [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- --]
 [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- -- --]
 [-- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- -- --]
 [-- -- -- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- -- --]
 [-- -- -- -- -- -- -- -- -- 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -- -- -- -- --]
 [-- -- -- -- -- -- -- -- -- -- -- -- 1 1 1 1 1 1 1 1 1 1 1 -- -- -- -- --]
 [-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1 1 1 1 1 1 1 1 -- -- -- -- --]
 [-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1 1 1 1 -- -- -- -- -- --]]

With masked array you could then just make the manipulations that you want and the only elements that are used are the unmasked ones.

Comments

0

Maybe this could help, where "a" its your array:

for i in range(len(a)):
    for j in range(len(a[i])):
        if(a[i][j]==1):
            print i,j

Comments

0

Probably numpy.nonzero is the easiest...

b = np.array(
[[0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,
    5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,
    6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
    6,  6,  6,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
    7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  8,  8,  8,  8,  8,  8,  8,
    8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
    9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
    9,  9,  9,  9,  9,  9,  9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
   10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11,
   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
   11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
   12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13,
   13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
   13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
   14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15,
   15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16,
   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
   16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
   17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
   18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
   18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
   19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
   21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22,
   22, 22, 23, 23, 23, 23]), array([ 5,  6,  5,  6,  7,  8,  9,  5,  6,  7,  8,  9, 10, 11, 12, 13,  4,
    5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  4,  5,  6,  7,  8,
    9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,  4,  5,  6,  7,  8,  9,
   10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,  3,  4,  5,  6,
    7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
   24, 25, 27,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
   17, 18, 19, 20, 21, 22, 23, 24, 25, 27,  3,  4,  5,  6,  7,  8,  9,
   10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27,
    3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
   20, 21, 22, 23, 24, 25, 27,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11,
   12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  2,  3,  4,
    5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
   22, 23, 24, 25,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  1,  2,  3,  4,  5,  6,
    7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
   24,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
   17, 18, 19, 20, 21, 22, 23, 24,  1,  2,  3,  4,  5,  6,  7,  8,  9,
   10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,  0,  1,
    2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
   19, 20, 21, 22, 23, 24,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
   11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,  2,  3,  4,  5,
    6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
   23,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
   21, 22, 23,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
   12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 15, 16, 17, 18, 19, 20,
   21, 22, 18, 19, 20, 21]))

Comments

0

The answer depends on what operation you want to perform. For a standard enumeration over indices and values there is a built in numpy iterator for this, namely numpy.ndenumerate, that is dimension agnostic.

import numpy as np

rng = np.random.default_rng()
a = rng.integers(2, size=(5,5))
print(a)

for ind, val in np.ndenumerate(a):
    if val == 1:
        print(ind)

giving

[[0 0 1 1 0]
 [1 1 1 1 0]
 [0 1 0 0 0]
 [1 1 0 0 1]
 [0 0 1 0 0]]
(0, 2)
(0, 3)
(1, 0)
(1, 1)
(1, 2)
(1, 3)
(2, 1)
(3, 0)
(3, 1)
(3, 4)
(4, 2)

Note that masked arrays are often a better way to operate on a certain set of entries.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.