1

I am having this array of arrays containing locations and time information:

my_array[:5]
[['39.921712' '116.472343' '0' '13' '39298.1462037037' '2007-08-04' '03:30:32']
 ['42.161385' '123.660773' '0' '221' '39298.6697337963' '2007-08-04' '16:04:25']
 ['42.161755' '123.66135' '0' '221' '39298.6697569444' '2007-08-04' '16:04:27']
 ['42.16194' '123.661638' '0' '221' '39298.6697685185' '2007-08-04' '16:04:28']
 ['39.907285' '116.448303' '0' '98' '39298.1581134259' '2007-08-04' '03:47:41']]

The first and second elements of each array are the position´s latitude and longitude.

I want to filter my_array to contain only those arrays where the latitude falls between 39.45 - 40.05 and the longitude fall between 115.416 - 117.5.

Expected out:

my_array_sorted
[['39.921712' '116.472343' '0' '13' '39298.1462037037' '2007-08-04' '03:30:32']
 ['39.907285' '116.448303' '0' '98' '39298.1581134259' '2007-08-04' '03:47:41']]
2
  • Have you tried code? Are there any errors? Commented Jun 24, 2020 at 21:18
  • Are these numpy arrays or python lists? Commented Jun 24, 2020 at 21:21

2 Answers 2

2

You can do this with a list comprehension:

data = [['39.921712', '116.472343', '0', '13', '39298.1462037037', '2007-08-04', '03:30:32'],
 ['42.161385', '123.660773', '0', '221', '39298.6697337963', '2007-08-04', '16:04:25'],
 ['42.161755', '123.66135', '0', '221', '39298.6697569444', '2007-08-04', '16:04:27'],
 ['42.16194', '123.661638', '0', '221', '39298.6697685185', '2007-08-04', '16:04:28'],
 ['39.907285', '116.448303', '0', '98', '39298.1581134259', '2007-08-04', '03:47:41'],]

filtered = [i for i in data
            if 39.45 < float(i[0]) < 40.05
            and 115.416 < float(i[1]) < 117.5]

print(filtered)

Output:

[['39.921712', '116.472343', '0', '13', '39298.1462037037', '2007-08-04', '03:30:32'],
['39.907285', '116.448303', '0', '98', '39298.1581134259', '2007-08-04', '03:47:41']]
Sign up to request clarification or add additional context in comments.

Comments

1

You should look at python's filter method that takes a predicate and your list, and keep only the elements that complete the predicate.

Here is an example taken from the documentation above:

number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)

# Output: [-5, -4, -3, -2, -1]

In your case it should looks like this:

my_array_sorted = list(filter(lambda array: 39.45 < array[0] < 40.05 and 115.416 < array[1] < 117.5))

2 Comments

interesting thing another user on SO showed me about a lot of the built-in python functions like filter, map, etc. is that comprehensions are in many cases a little faster, also more "pythonic", and a bit easier to read
@TenaciousB callbacks are a bit hard to understand when you are new to python. I'm personnaly used to use lambdas as I come from Java where we have Consumer, Predicate, etc. But basically both approaches work and it's just a matter of taste.

Your Answer

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.