简体   繁体   中英

Python: How to continuously remove the minimum of a list and add it to another list?

I'm currently a novice with Python and I'm trying to learn it efficiently. A project I created for myself requires me to take the minimum value of a list, then add it to another list, and then finally remove that minimum value and redo that process until there are no more values in the original list.

For example, if I have

list = [0, 3, 2, 1, 4, 2, 4, 5, 5]

I want to retrieve a list with the values:

list2 = [0, 1, 2, 2, 3, 4, 4, 5, 5] #(in this specific order)
list = [] # empty because values have been deleted. 

Here's the code I already have:

count = 0
counter = len(group0)
while counter > 1:
    while count < len(group0):
         if min(group0) == group0[count]:
            finalgroup0.append(group0[count]) #finalgroup0 is an empty list at the start.
            group0.remove(group0[count]) #group0 has all of the values. 
            count += 1
        else:
            count += 1
    counter -= 1

Note: The only reason I am deleting the value in list is so that I can take the min of the whole list once again. If this isn't necessary, please enlighten me on it. Also, this code worked to a certain extent, but it did not finish through the whole entire list. This is why I added the while loop outside so that once it reaches one it would be done, but that did not work either. I believe this is because it is checking all values for 'count' and checking if it is a min, and that is why the counter value needs to be higher. However, if I increase the counter value, then there is a 'list index, out of range' error.

I understand that my code is not the most efficient, but I've been at this and I have tried using a for loop and others but none of them have worked. If someone could please help me out, I would be greatly appreciative.

Thanks in advance.

list = [0, 3, 2, 1, 4, 2, 4, 5, 5]
list.sort()
list2, list = list, []

Use the sort () method

list.sort()
print(list)

It will save you having an additional variable (list2)

There are many ways to do it, it is especially true as you are using python.

If your list is small, usually we really don't care about performance too much. Using a built-in min and delete func is okay, and if you just need a correct result, like others have mentioned, sort it and copy to another list, that will do.

If you have a really big list, the above two methods would not be recommended, as out-of-cache issue makes them even slower than expected. You gotta need a heap/priority queue.

c = [3, 2, 1, 4, 2, 4, 5, 5]

h = []
while c:
    heapq.heappush(h, c.pop())

result = []
while h:
    result.append(heapq.heappop(h))

One more thing, never use list to name a variable, it will overwrite built-in list function.

You can use a while loop to getting the minimum item from group0 and appending it to finalgroup0 and deleting the item from group0 until group0 becomes empty:

from operator import itemgetter
group0 = [0, 3, 2, 1, 4, 2, 4, 5, 5]
finalgroup0 = []
while group0:
    i, n = min(enumerate(group0), key=itemgetter(1))
    finalgroup0.append(n)
    del group0[i]
    print('finalgroup0:', finalgroup0)
    print('group0:', group0)

This outputs:

finalgroup0: [0]
group0: [3, 2, 1, 4, 2, 4, 5, 5]
finalgroup0: [0, 1]
group0: [3, 2, 4, 2, 4, 5, 5]
finalgroup0: [0, 1, 2]
group0: [3, 4, 2, 4, 5, 5]
finalgroup0: [0, 1, 2, 2]
group0: [3, 4, 4, 5, 5]
finalgroup0: [0, 1, 2, 2, 3]
group0: [4, 4, 5, 5]
finalgroup0: [0, 1, 2, 2, 3, 4]
group0: [4, 5, 5]
finalgroup0: [0, 1, 2, 2, 3, 4, 4]
group0: [5, 5]
finalgroup0: [0, 1, 2, 2, 3, 4, 4, 5]
group0: [5]
finalgroup0: [0, 1, 2, 2, 3, 4, 4, 5, 5]
group0: []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM