-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]]
khelwood
  • 55,782
  • 14
  • 81
  • 108
NeroVero
  • 17
  • 3
  • There is a filter function that does that => [here](https://docs.python.org/3/library/functions.html#filter). You can just Google, "Python 3 filter". – mutantkeyboard Dec 01 '17 at 11:53
  • Tag `numpy` if that is what you're asking about. – khelwood Dec 01 '17 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. – hpaulj Dec 01 '17 at 18:00

2 Answers2

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

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
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]
Víctor López
  • 819
  • 1
  • 7
  • 19