Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erase whole array Python

How do I erase a whole array, leaving it with no items?

I want to do this so I can store new values in it (a new set of 100 floats) and find the minimum.

Right now my program is reading the minimum from sets before I think because it is appending itself with the previous set still in there. I use .append by the way.

like image 440
pjehyun Avatar asked Aug 17 '10 04:08

pjehyun


People also ask

How do I delete an entire NumPy array?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.

How do you reset an array to zero in Python?

If you are using numpy arrays then the_array[...] = 0 should do the trick.

How do you delete all items in a list Python?

Using list.list. clear() is the recommended solution in Python 3 to remove all items from the list.


2 Answers

Note that list and array are different classes. You can do:

del mylist[:] 

This will actually modify your existing list. David's answer creates a new list and assigns it to the same variable. Which you want depends on the situation (e.g. does any other variable have a reference to the same list?).

Try:

a = [1,2] b = a a = [] 

and

a = [1,2] b = a del a[:] 

Print a and b each time to see the difference.

like image 116
Matthew Flaschen Avatar answered Oct 14 '22 03:10

Matthew Flaschen


It's simple:

array = [] 

will set array to be an empty list. (They're called lists in Python, by the way, not arrays)

If that doesn't work for you, edit your question to include a code sample that demonstrates your problem.

like image 31
David Z Avatar answered Oct 14 '22 01:10

David Z