-2

I have a numpy array. I want to remove first element and print the remaining array. Then I want to put back first element and remove second one and print list and then I put back second element and and romove third one. I want to repeat same procedure for whole array. But I am not sure how to do that.

`a= np.array([11,12,13,14,15])
for i in range(len(a))
    E_list= a[]`
learn_python
  • 17
  • 1
  • 4
  • I tried to use list.pop command in for loop considering a as a list. But it is not giving the required result. ans np.delete is also not appropriate – learn_python Jul 16 '18 at 09:51
  • You might find these helpful: [Understanding Python's slice notation](https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation) and [Multiple Slices With Python](https://stackoverflow.com/questions/38895781/multiple-slices-with-python) – Georgy Jul 16 '18 at 10:41

2 Answers2

0

First a simple list approach:

In [41]: alist = [11,12,13,14,15]
In [42]: for i in range(len(alist)):
    ...:     a = alist[:] # copy
    ...:     a[i:i+1]=[]  # remove
    ...:     print(a)
    ...:     
[12, 13, 14, 15]
[11, 13, 14, 15]
[11, 12, 14, 15]
[11, 12, 13, 15]
[11, 12, 13, 14]

By working a copy each loop I don't have to put the element back on the original list. If the assignment really required reinsertion I could do (this took a bit more trial-and-error):

In [46]: alist = [11,12,13,14,15]
In [47]: for i in range(len(alist)):
    ...:     a = alist[i]
    ...:     alist[i:i+1]=[]  # remove
    ...:     print(alist)
    ...:     alist[i:i]=[a]
    ...:     
[12, 13, 14, 15]
[11, 13, 14, 15]
[11, 12, 14, 15]
[11, 12, 13, 15]
[11, 12, 13, 14]

An array version of this looks simpler:

In [48]: arr = np.array( [11,12,13,14,15])
In [49]: for i in range(len(alist)):
    ...:     a = np.delete(arr, i)  # makes a copy
    ...:     print(a)
    ...:     
    ...:     
[12 13 14 15]
[11 13 14 15]
[11 12 14 15]
[11 12 13 15]
[11 12 13 14]

delete makes a new array, so we don't have to make an explicit copy, nor do we have to reinsert the values. Under the covers delete is doing something like:

In [50]: for i in range(len(alist)):
    ...:     a = np.concatenate((arr[:i],arr[i+1:]))
    ...:     print(a)

Actually we could do the same thing with the list

In [54]: [alist[:i]+alist[i+1:] for i in range(len(alist))]
Out[54]: 
[[12, 13, 14, 15],
 [11, 13, 14, 15],
 [11, 12, 14, 15],
 [11, 12, 13, 15],
 [11, 12, 13, 14]]
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

Iterating over a list and saving the conversion to an array until the end will be your friend here, as lists are better optimized for iteration. This exercise is all about list slices. You'll want to iterate over the list and on each iteration, make a pair of slices of the original list around the index, and then print the concatenation of the slices. This does not actually modify the original list, but rather creates new lists based on the desired parameters.

In [1]: alist = [1,2,3,4,5]

In [2]: for i, a in enumerate(alist):
   ...:     new_list = alist[:i] + alist[i+1:]
   ...:     print(new_list)
   ...:
[2, 3, 4, 5]
[1, 3, 4, 5]
[1, 2, 4, 5]
[1, 2, 3, 5]
[1, 2, 3, 4]

If necessary, you can replicate this with a numpy.array, and the slicing syntax is identical. But array concatenation requires the numpy.concatenate function and is a little uglier.

In [20]: np_arr = np.array([1,2,3,4,5])

In [21]: for i, a in enumerate(np_arr):
    ...:     new_arr = np.concatenate((np_arr[:i], np_arr[i+1:]))
    ...:     print(new_arr)
    ...:
[2 3 4 5]
[1 3 4 5]
[1 2 4 5]
[1 2 3 5]
[1 2 3 4]
mlaponsky
  • 13
  • 1
  • 3