2

Is there a simple way to access the contents of an array with multiple conditions?

For example, lets say

a=[1,2,3,4,5,6,7,8,9,10]

But I'm only interested in values ranging from 2 to 9 (inclusive)

So I want to know two things:

1) The number of elements that satisfy these conditions (that is, where a>1 and a<10), so in this example it would be 8.

2) A new array with the values that satify those conditions. In this case,

new_a=[2,3,4,5,6,7,8,9]

I still suck at indexing in Python :/

5
  • Use a list comprehension. Commented Feb 17, 2016 at 22:52
  • you can provide your own function to the sort method in order to compare 2 elements, default is cmp. Commented Feb 17, 2016 at 22:53
  • 1
    @PauloScardine What does this have to do with sortingz? Commented Feb 17, 2016 at 22:53
  • @Barmar I misread the question as sorting with multiple conditions when it is really just about filtering. Commented Feb 17, 2016 at 22:57
  • Sorry, I probably could have worded that better. Commented Feb 17, 2016 at 22:58

4 Answers 4

2
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1,2,3,4,5,6,7,8,9]
>>> new_a = [x for x in a if x > 1 and x < 10]
>>> print new_a
[2, 3, 4, 5, 6, 7, 8, 9]
>>> print len(new_a)
8
Sign up to request clarification or add additional context in comments.

5 Comments

I originally answered the question teaching about slicing as well, but OP isn't actually worried about indexes so this answer is correct.
You can simplify the condition to 1 < x < 10
I like this one a lot! Didnt even know you could use if and for's in brackets like that. Thanks.
@JaronThatcher: Well, now that I see that, my answer is useless.
@JohnAlperto That's called List Comprehension and is one of the best parts of Python :)
2

The built in filter function was built for just that.

def filter_fun(x):
        return 1<x<10
a = range(10)
new_a = filter(filter_fun, a)
print a
print new_a
print len(new_a)

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9]
8

1 Comment

i prefer this answer, uses an inbuilt function
2

Since the question is tagged as numpy and array, how about this:

In [1]: import numpy as np

In [2]: a = np.array([1,2,3,4,5,6,7,8,9,10])

In [3]: a[(a>1) & (a<10)]
Out[3]: array([2, 3, 4, 5, 6, 7, 8, 9])

1 Comment

What the heck. I knew you could do that in MATLAB but not in Python. Awesome. I'd upvote if I could. Edit: Wait, I can :)
0

For some arbitrary indices, you can use itemgetter to build a function

>>> from operator import itemgetter
>>> interesting_things = itemgetter(2, 3, 4, 5, 6, 7, 8, 9)
>>> a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
>>> interesting_things(a)
('c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')

otherwise you can write an actual function for extra flexibility

>>> def interesting_things(L):
...     return [item for i, item in enumerate(L) if 1 < i < 10]
... 
>>> interesting_things(a)
['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Regardless, you should put the logic into a function so it's easy to test and change

Comments

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.