-3

I have an array

a=[0, 10, 20, 30, 40, 50, 60] 

I selected the 2nd and 3rd element with

a[[1,2]] 

obtaining

array([10, 20])

How do I select the other elements of a except the elements I've already selected?

That is, I want to obtain:

array([0, 30, 40, 50, 60])

Logically, should be something like

a[![1,2]]
3
  • There is a filter function that does that => here. You can just Google, "Python 3 filter". Commented Dec 1, 2017 at 11:53
  • Tag numpy if that is what you're asking about. Commented Dec 1, 2017 at 11:54
  • a as shown (but not named) is a list. a[[1,2]] gives an error. If a really is a numpy array, you should show that in your code. Commented Dec 1, 2017 at 18:00

2 Answers 2

1

Like this:

a=[0, 10, 20, 30, 40, 50, 60]

b = a[1:3]
c =[x for x in a if x not in b]

print(a)
print(b)
print(c)

Output:

[0, 10, 20, 30, 40, 50, 60]
[10, 20]
[0, 30, 40, 50, 60]

If order does not matter, you can stuff the list in a set and use these set operations: yourSet.union(otherSet) , yourSet.intersect(otherSet) , yourSet.difference(otherSet) , etc

Sign up to request clarification or add additional context in comments.

3 Comments

This uses plain old Python.
@MateenUlhaq yes? If you have numpy and a hammer and see a nail - what would you do?
Smack it with the numpy of course!
0

In case on python 2.7, the easiest solution is that:

a=[0, 10, 20, 30, 40, 50, 60]
c=[1,2]
values = [a[i] for i, x in enumerate(a) if i not in c]
print values
[0, 30, 40, 50, 60]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.