简体   繁体   English

Python:如何连续删除列表的最小值并将其添加到另一个列表?

[英]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. 我目前是Python的新手,我正在尝试有效地学习它。 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. 这就是为什么我在外部添加了while循环,以便一旦它到达一个就可以完成,但是那也不起作用。 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. 我相信这是因为它正在检查“ count”的所有值并检查它是否是最小值,这就是为什么计数器值需要更高的原因。 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. 我知道我的代码不是最有效的,但是我一直在这样做,并且尝试使用for循环和其他方法,但是没有一个起作用。 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 使用sort()方法

list.sort()
print(list)

It will save you having an additional variable (list2) 它将为您节省一个额外的变量(list2)

There are many ways to do it, it is especially true as you are using python. 有很多方法可以做到这一点,当您使用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. 可以使用内置的min和delete函数,并且,如果您只需要一个正确的结果(如其他人所提到的那样),则将其排序并复制到另一个列表中就可以了。

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. 还有一件事,永远不要使用list来命名变量,它会覆盖内置列表功能。

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: 您可以使用while循环从group0获取最小项,并将其追加到finalgroup0然后从group0删除该项,直到group0空:

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: []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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