Index All *Except* One Item in Python

Index all *except* one item in python

For a list, you could use a list comp. For example, to make b a copy of a without the 3rd element:

a = range(10)[::-1]                       # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
b = [x for i,x in enumerate(a) if i!=3] # [9, 8, 7, 5, 4, 3, 2, 1, 0]

This is very general, and can be used with all iterables, including numpy arrays. If you replace [] with (), b will be an iterator instead of a list.

Or you could do this in-place with pop:

a = range(10)[::-1]     # a = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
a.pop(3) # a = [9, 8, 7, 5, 4, 3, 2, 1, 0]

In numpy you could do this with a boolean indexing:

a = np.arange(9, -1, -1)     # a = array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
b = a[np.arange(len(a))!=3] # b = array([9, 8, 7, 5, 4, 3, 2, 1, 0])

which will, in general, be much faster than the list comprehension listed above.

How to select all elements in a NumPy array except for a sequence of indices

This is what numpy.delete does. (It doesn't modify the input array, so you don't have to worry about that.)

In [4]: np.delete(x, exclude)
Out[4]: array([ 0, 20, 40, 60])

How can I remove all the numbers in a list except for 1 number (Python)?

You've got a few issues. First, you're trying to modify the list in place, as you're trying to process it. You can fix that by changing:

for i in myList:

to:

for i in myList[:]:

That will allow your code to give the desired result. It makes a "copy" of the list over which to iterate, so you can modify the original list without messing up your iteration loop.

The other thing to note is that you assign a value to i in the for loop, but then you manually change it after your if-else block. That change gets discarded when you go back to the top of the loop.

Also, you're else: continue prevents incrementing i, but it doesn't matter, because that incremented value was just getting tossed anyway.

So... commenting out some of the unnecessary stuff gives:

myList = [0,1,2,3,4,5]
# i = 0
for i in myList[:]:
print(i)
if i != 3:
myList.remove(i)
# else:
# continue
# i += 1
print(myList)

Retrieving all but one elements in a list of lists (Python)

Loop over the indices of the outer list (list_of_lists) and exclude the list at that index on each iteration of the loop.

Example:

for i in range(len(list_of_lists)):
remaining_lists = list_of_lists[0:i] + list_of_lists[i+1:]
train_classifier(remaining_lists)
excluded_list = list_of_lists[i]
test_classifier(excluded_list)

Resulting IDs of lists per iteration:

Remaining    | Excluded
-------------+---------
[2, 3, 4, 5] | 1
[1, 3, 4, 5] | 2
[1, 2, 4, 5] | 3
[1, 2, 3, 5] | 4
[1, 2, 3, 4] | 5

NumPy slicing: All except one array entry

I think the simplest would be

np.prod(x[:i]) * np.prod(x[i+1:])

This should be fast and also works when you don't want to or can't modify x.

And in case x is multidimensional and i is a tuple:

x_f = x.ravel()
i_f = np.ravel_multi_index(i, x.shape)
np.prod(x_f[:i_f]) * np.prod(x_f[i_f+1:])

How to get everything from the list except the first element using list slicing

You can just do [1:].
This will work on both versions.

A quick way to return list without a specific element in Python

>>> suits = ["h","c", "d", "s"]
>>> noclubs = list(suits)
>>> noclubs.remove("c")
>>> noclubs
['h', 'd', 's']

If you don't need a seperate noclubs

>>> suits = ["h","c", "d", "s"]
>>> suits.remove("c")


Related Topics



Leave a reply



Submit