user8843
user8843

Reputation: 11

Is there a way to filter coordinates in an array

This is my array:

array = {(0,0):[1], (0,1):[0], (0,2):[1], (0,3):[0], (0,4):[1], (0,5):[0],
(1,0):[1], (1,1):[0], (1,2):[1], (1,3):[0], (1,4):[1], (1,5):[0]}

Does anyone know of a method to filter where x coordinate is 0 into a separate array like this.

filtered_array1 = {(0,0):[1], (0,1):[0], (0,2):[1], (0,3):[0], (0,4):[1], (0,5):[0]}

Similarly, is there a way to filter through the box linked to the coordinates such as when it is equal to 1; like this

filtered_array2 = {(0,0):[1], (0,2):[1], (0,4):[1], (1,0):[1], (1,2):[1], (1,4):[1]}

Upvotes: 0

Views: 269

Answers (3)

Taku
Taku

Reputation: 33744

You can use two dict comprehension for both of these. (Side note: “the box linked to the coordinates” that is called a list)

array = {(0,0):[1], (0,1):[0], (0,2):[1], (0,3):[0], (0,4):[1], (0,5):[0],
(1,0):[1], (1,1):[0], (1,2):[1], (1,3):[0], (1,4):[1], (1,5):[0]}

filtered_array1 = {k: v for k, v in array.items() if k[0] == 0}
filtered_array2 = {k: v for k, v in array.items() if v[0] == 1}

print(filtered_array1)
print(filtered_array2)

In addition, as commented by PM 2Ring, building only one filtered array could be done with a dict comprehension, but building multiple arrays would require iterating the original array multiple times, making it inefficient. You can use a traditional for loop instead.

array = {(0,0):[1], (0,1):[0], (0,2):[1], (0,3):[0], (0,4):[1], (0,5):[0],
(1,0):[1], (1,1):[0], (1,2):[1], (1,3):[0], (1,4):[1], (1,5):[0]}

filtered_array1 = {}
filtered_array2 = {}

for k, v in array.items():
    if k[0] == 0:
        filtered_array1[k] = v
    elif k[0] == 1:
        filtered_array2[k] = v

As proven by timeit, the former returned a timeit value of 0.003447920083999634 While the latter only 0.002893589437007904. Although it’s not much difference right now, but when you have 3, 4, or 10 filtered_arrays, and the array gets way bigger. The difference will be dramatic.

Try it here: https://ideone.com/rSvC0i

Upvotes: 2

Hat
Hat

Reputation: 570

You can use dictionary comprehensions.

Also that's a dictionary, not a list or (numpy) array.

d = {(0, 0): [1], (0, 1): [0], (0, 2): [1], (0, 3): [0], (0, 4): [1], (0, 5): [0],
 (1, 0): [1], (1, 1): [0], (1, 2): [1], (1, 3): [0], (1, 4): [1], (1, 5): [0]}

result_1 = {k: v for k, v in d.items() if k[0] == 0}
desired_result_1 = {(0, 0): [1], (0, 1): [0], (0, 2): [1], (0, 3): [0], (0, 4): [1],
                (0, 5): [0]}
print(result_1 == desired_result_1)  # prints True

result_2 = {k: v for k, v in d.items() if v[0] == 1}
desired_result_2 = {(0, 0): [1], (0, 2): [1], (0, 4): [1], (1, 0): [1], (1, 2): [1],
                (1, 4): [1]}
print(result_2 == desired_result_2)  # prints True

Upvotes: 0

Prasad
Prasad

Reputation: 6034

array = {(0,0):[1], (0,1):[0], (0,2):[1], (0,3):[0], (0,4):[1], (0,5):[0],
(1,0):[1], (1,1):[0], (1,2):[1], (1,3):[0], (1,4):[1], (1,5):[0]}

dict1 = {(k1, k2): v for (k1, k2), v in array.items() if k1 == 0}
print(dict1)
# {(0, 1): [0], (0, 0): [1], (0, 5): [0], (0, 4): [1], (0, 3): [0], (0, 2): [1]}


dict2 = {(k1, k2): v for (k1, k2), v in array.items() if v[0] == 1}
print(dict2)
# {(1, 2): [1], (0, 0): [1], (0, 4): [1], (1, 0): [1], (0, 2): [1], (1, 4): [1]}

Upvotes: 1

Related Questions