Reputation: 21
I'm trying to create a function that finds the minimum value of a list and removes it, only without using .remove or min. I have the code to find the minimum value:
userlist = [1,2,3,4,5]
smallest = userlist[0]
def removeMin():
for i in range(1, len(userlist)):
if userlist[i] < smallest:
smallest = userlist[i]
I've tried using .pop but clearly that does not work because smallest is a variable and not a position. Any help would be appreciated.
Upvotes: 1
Views: 9510
Reputation: 19
Here is simple approach without any complex loops and functions
arr=[1,20,5,78,30]
count=0
element=int(input("enter the element you want to remove:"))
for i in range(len(arr)):
if(arr[i]==element):
count=count+1
if(count>=1):
pos=arr.index(element)
arr1 = arr[:pos]+arr[pos+1:]
print(arr1)
else:
print("element not found in array")
Upvotes: 0
Reputation: 328
Well, if your main task is merely supposed to find out and remove the smallest item in a certain list, I mean not care about the order of those elements. Maybe you can try python's heapq
module.
It provides an implementation that transforms list into a heap, in-place. Then you can remove the smallest element of the heap in a simple and efficient way:
Here is a sample:
import heapq
#alist could be a disordered list, it doesn't matter
alist = [10, 2, 4, 5, 8, 50]
heapq.heapify(alist) #transform alist to heap, in-place. That means None is returned
smallest = heapq.heappop(alist) #pop the smallest element of alist, here is 2
print smallest #output: 2
And you can simply go on, to find out and remove the 2nd small(3rd small, 4th small, ...) element of the remaining alist
, using heapq.heappop(alist)
, until an IndexError
raised(nothing left in alist):
print heapq.heappop(alist) #output: 4
print heapq.heappop(alist) #output: 5
print heapq.heappop(alist) #output: 8
print heapq.heappop(alist) #output: 10
print heapq.heappop(alist) #output: 50
If continue... IndexError
raised:
In [7]: heapq.heappop(alist)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/home/chiyu/<ipython-input-7-8faac314c419> in <module>()
----> 1 heapq.heappop(alist)
IndexError: index out of range
In [8]:
You can put the heappop
into a try/except
block:
import heapq
alist = [10, 2, 4, 5, 8, 50]
heapq.heapify(alist)
while True:
try:
smallest = heapq.heappop(alist)
except IndexError:
break
print smallest
The output would be:
2
4
5
8
10
50
There's one thing deserved to be mentioned again:
Because when you call heapq.heapify(alist)
orders of those elements have changed:
In [8]: alist = [10, 2, 4, 5, 8, 50]
In [9]: heapq.heapify(alist)
In [10]: alist
Out[10]: [2, 5, 4, 10, 8, 50]
Upvotes: 0
Reputation:
If you are iterating through a python object, there are much better ways to iterate than using range. Any iterable can be looped through like so:
for x in my_list:
pass # do something
The above will step through every element set the current iteration to x
. However, if you want the index as well, use the Python enumerate()
built-in, which instead of giving just the item, also gives the current index, like so:
for index, item in enumerate(my_list):
pass # do something
So for your function you want something like:
def removeMin(my_list):
smallestIndex = 0
smallest = my_list[0]
for i,val in enumerate(my_list):
if val < smallest:
smallest = val
smallestIndex = i
my_list.pop(smallestIndex)
return my_list
edit: if it is a puzzle, the most devious way of doing it is with a list comprehension, like so:
userlist.pop(userlist.index(-max([-u for u in userlist])))
This:
[-u for u in userlist]
max
imum of that list (which is the minimum of the normal list) max(...)
userlist.index(-...)
userlist.pop(...)
Voila, not a .remove
or a min
in sight!
Upvotes: 1
Reputation: 16940
Here is one straight forward approach:
#!/usr/bin/python
userlist = [10,2,30,4,5]
def rm_min(lis):
minn = None
ind = None
for i, j in enumerate(lis):
if minn and j < minn:
minn = j
ind = i
elif not minn:
minn = j
else:
pass
return [ y for x,y in enumerate(userlist) if x != ind]
print rm_min(userlist)
Output:
[10, 30, 4, 5]
Upvotes: 1