Python 从列表中删除最低值

Python 从列表中删除最低值,python,list,Python,List,我试图编写一个python程序,从列表中删除25%的最低值,并返回原始的未排序列表。比如, Input : [1,5,6,72,3,4,9,11,3,8] Output : [5,6,72,4,9,11,8] 我试着做: l = [1,5,6,72,3,4,9,11,3,8] def drop(k): while len(l)!=0 and k > 0: k = k - 1 l.sort(reverse = True) l.p

我试图编写一个python程序,从列表中删除25%的最低值,并返回原始的未排序列表。比如,

Input : [1,5,6,72,3,4,9,11,3,8] 
Output : [5,6,72,4,9,11,8]
我试着做:

l = [1,5,6,72,3,4,9,11,3,8]

def drop(k):
     while len(l)!=0 and k > 0:
        k = k - 1
        l.sort(reverse = True)
        l.pop()
 return l

k = math.ceil(len(l) * 0.25)
drop (k)

它返回[72,11,9,8,6,5,4],但有没有不进行排序的方法?

。然后,过滤原始列表的内容

import heapq, copy
s = [1,5,6,72,3,4,9,11,3,8] 
new_s = copy.deepcopy(s)
heapq.heapify(s)
count = 0
last_items = set()
while count/float(len(new_s)) <= 0.25:
  last_items.add(heapq.heappop(s))
  count += 1

final_s = [i for i in new_s if i not in last_items]

您可以使用
heapq
并不断弹出元素,直到移除容器的25%。然后,过滤原始列表的内容

import heapq, copy
s = [1,5,6,72,3,4,9,11,3,8] 
new_s = copy.deepcopy(s)
heapq.heapify(s)
count = 0
last_items = set()
while count/float(len(new_s)) <= 0.25:
  last_items.add(heapq.heappop(s))
  count += 1

final_s = [i for i in new_s if i not in last_items]

您不需要反向排序并找到最小的元素。使用列表
l
上的
min
,该列表返回
l
中的最小值,并使用
l删除。方便地删除

import math
l = [1,5,6,72,3,4,9,11,3,8]

def drop(k):
     while len(l)!=0 and k > 0:
        k = k - 1
        l.remove(min(l))
     return l

k = math.ceil(len(l) * 0.25)
print(drop (k))
# [5, 6, 72, 4, 9, 11, 8]

您不需要反向排序并找到最小的元素。使用列表
l
上的
min
,该列表返回
l
中的最小值,并使用
l删除。方便地删除

import math
l = [1,5,6,72,3,4,9,11,3,8]

def drop(k):
     while len(l)!=0 and k > 0:
        k = k - 1
        l.remove(min(l))
     return l

k = math.ceil(len(l) * 0.25)
print(drop (k))
# [5, 6, 72, 4, 9, 11, 8]

一种方法是这非常慢,尤其是对于较长的列表

quart_len = int(0.25*len(l))
for i in range(quart_len):
     l.remove(min(l))
一种更快的方法:

import numpy as np
from math import ceil

l = [1,5,6,72,3,4,9,11,3,8]
sorted_values = np.array(l).argsort()
l_new = [l[i] for i in range(len(l)) if i in sorted_values[int(ceil(len(l)/4.)):]]
另一种方法:

l = np.array(l)
l = list(l[l > sorted(l)[len(l)/4]])

一种方法是这非常慢,尤其是对于较长的列表

quart_len = int(0.25*len(l))
for i in range(quart_len):
     l.remove(min(l))
一种更快的方法:

import numpy as np
from math import ceil

l = [1,5,6,72,3,4,9,11,3,8]
sorted_values = np.array(l).argsort()
l_new = [l[i] for i in range(len(l)) if i in sorted_values[int(ceil(len(l)/4.)):]]
另一种方法:

l = np.array(l)
l = list(l[l > sorted(l)[len(l)/4]])
输出:

[5, 6, 72, 4, 9, 11, 8]
[5, 6, 72, 4, 9, 11, 8]
输出:

[5, 6, 72, 4, 9, 11, 8]
[5, 6, 72, 4, 9, 11, 8]
有O(n)个解决方案。其中一个introselect在
分区
argpartition
函数的numpy中实现:

>>> data = [1,5,6,72,3,4,9,11,3,8] 
>>> 
>>> k = int(round(len(data) / 4))
>>>
>>> import numpy as np
>>> dnp = np.array(data)
>>> drop_them = np.argpartition(dnp, k)[:k]
>>> keep_them = np.ones(dnp.shape, dtype=bool)
>>> keep_them[drop_them] = False
>>> result = dnp[keep_them].tolist()
>>> 
>>> result
[5, 6, 72, 4, 9, 11, 3, 8]
请注意,此方法保留
3
s中的一个,并删除另一个,以便在
k
元素处获得精确的分割

如果您想对所有
3
s一视同仁,您可以这样做

>>> boundary = np.argpartition(dnp, k)[k]
>>> result = dnp[dnp > dnp[boundary]]
>>> 
>>> result
array([ 5,  6, 72,  4,  9, 11,  8])
有O(n)个解决方案。其中一个introselect在
分区
argpartition
函数的numpy中实现:

>>> data = [1,5,6,72,3,4,9,11,3,8] 
>>> 
>>> k = int(round(len(data) / 4))
>>>
>>> import numpy as np
>>> dnp = np.array(data)
>>> drop_them = np.argpartition(dnp, k)[:k]
>>> keep_them = np.ones(dnp.shape, dtype=bool)
>>> keep_them[drop_them] = False
>>> result = dnp[keep_them].tolist()
>>> 
>>> result
[5, 6, 72, 4, 9, 11, 3, 8]
请注意,此方法保留
3
s中的一个,并删除另一个,以便在
k
元素处获得精确的分割

如果您想对所有
3
s一视同仁,您可以这样做

>>> boundary = np.argpartition(dnp, k)[k]
>>> result = dnp[dnp > dnp[boundary]]
>>> 
>>> result
array([ 5,  6, 72,  4,  9, 11,  8])


他们希望维持原来的秩序。当然。你真的应该把
last\u项目设置为一组。他们希望保持原来的顺序。当然。你真的应该把
last_items
设置为一组。我甚至不能开始描述这个解决方案的速度有多慢。@Aran Fey我知道,但速度要求从未指定过,而且似乎只是一个简短的列表。人们每天都从StackOverflow复制/粘贴代码-如果你的代码在很多现实场景中都非常慢,我想你的答案至少应该有一个免责声明。我甚至不能开始描述这个解决方案有多慢。@Aran Fey我知道,但速度要求从未被指定,而且似乎只是一个简短的列表。人们每天从StackOverflow复制/粘贴代码-如果您的代码在许多真实场景中速度非常慢,我认为您的答案至少应该对此有一个免责声明。您可以通过将
l2
转换为一个集合来实现显著的加速,或者,如果我在l2[ln+1://code>中,则至少对
进行否定,如果我不在l2[:ln+1]
中,则至少对
进行否定。我们仍然在这里进行排序。通过将
l2
转换为一个集合,或者如果我在l2[ln+1://code>中,则至少对
进行否定,如果我不在l2[:ln+1]
。我们仍在这里排序。在同一个列表上反复调用
remove
min
速度非常慢。此解决方案完全不适用于较长的列表。在我看来,这相当于O(n^2)。而且,一个接一个地删除随机元素是非常低效的。事实上,我首先使用了pop,因为它是O(1)。“我可以再提一个建议吗?”Aran Fey我所做的只是通过解释代码而不是构建新的东西来告诉OP他哪里出了问题。OP需要理解他的代码和错误。其他答案都不能解释这一点。看,问题是,排序实际上比在同一个列表上调用
min
一千次要好。(也就是说,如果将排序移出循环。)重复调用同一列表上的
remove
min
,速度非常慢。此解决方案完全不适用于较长的列表。在我看来,这相当于O(n^2)。而且,一个接一个地删除随机元素是非常低效的。事实上,我首先使用了pop,因为它是O(1)。“我可以再提一个建议吗?”Aran Fey我所做的只是通过解释代码而不是构建新的东西来告诉OP他哪里出了问题。OP需要理解他的代码和错误。其他答案都不能解释这一点。看,问题是,排序实际上比在同一个列表上调用
min
一千次要好。(即,如果将排序移出循环。)