Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数组python中查找前k个元素而不使用heapq或排序_Python - Fatal编程技术网

在数组python中查找前k个元素而不使用heapq或排序

在数组python中查找前k个元素而不使用heapq或排序,python,Python,我试图在python中查找列表的前k个元素,而不使用heapq或对列表进行排序 这就是我试过的 list = [20,4,67,22,445,1,34] k = 3 newList=[] for i in range(0,k): newList.append(list[i]) for i in list: mini = min(newList) if i <= mini: continue else: newList.rem

我试图在python中查找列表的前k个元素,而不使用heapq或对列表进行排序

这就是我试过的

list = [20,4,67,22,445,1,34]
k = 3
newList=[]

for i in range(0,k):
    newList.append(list[i])
for i in list:
    mini = min(newList)
    if i <= mini:
        continue
    else:
        newList.remove(mini)
        newList.append(i)
print newList
list=[20,4,67,22445,1,34]
k=3
新列表=[]
对于范围(0,k)内的i:
追加(列表[i])
对于列表中的i:
mini=min(新列表)

如果i添加一些跟踪时,问题很明显:

>>> list = [20,4,67,22,445,1,34]
>>> k = 3
>>> newList=[]
>>> 
>>> for i in range(0,k):
...     newList.append(list[i])
... 
>>> for i in list:
...     mini = min(newList)
...     if i <= mini:
...         continue
...     else:
...         print newList
...         print "Replacing {0} with {1}".format(mini, i)
...         newList.remove(mini)
...         newList.append(i)
...         print newList
...         print '-' * 20
... 
[20, 4, 67]
Replacing 4 with 20
[20, 67, 20]
--------------------
[20, 67, 20]
Replacing 20 with 67
[67, 20, 67]
--------------------
[67, 20, 67]
Replacing 20 with 22
[67, 67, 22]
--------------------
[67, 67, 22]
Replacing 22 with 445
[67, 67, 445]

不过,我不会为您的列表命名。

添加一些跟踪时,问题很明显:

>>> list = [20,4,67,22,445,1,34]
>>> k = 3
>>> newList=[]
>>> 
>>> for i in range(0,k):
...     newList.append(list[i])
... 
>>> for i in list:
...     mini = min(newList)
...     if i <= mini:
...         continue
...     else:
...         print newList
...         print "Replacing {0} with {1}".format(mini, i)
...         newList.remove(mini)
...         newList.append(i)
...         print newList
...         print '-' * 20
... 
[20, 4, 67]
Replacing 4 with 20
[20, 67, 20]
--------------------
[20, 67, 20]
Replacing 20 with 67
[67, 20, 67]
--------------------
[67, 20, 67]
Replacing 20 with 22
[67, 67, 22]
--------------------
[67, 67, 22]
Replacing 22 with 445
[67, 67, 445]

不过,我不会为您的列表命名。

您只需按照以下方式操作即可:

a = [20,4,67,22,445,1,34]
k = 3
newList=[]

for i in range(k):
    pos = a.index(max(a))
    newList.append(a[pos])
    a.pop(pos)

>>> print newList
[67, 445, 34]

您只需按照以下方式进行操作:

a = [20,4,67,22,445,1,34]
k = 3
newList=[]

for i in range(k):
    pos = a.index(max(a))
    newList.append(a[pos])
    a.pop(pos)

>>> print newList
[67, 445, 34]

开始时新列表中有67个,开始时新列表中有67个永不弹出,而hughdbrown的解决方案中有一个bug我注意到了。 如果列表中有类似的条目,那么结果将只显示其中一个条目。
例如,如果列表是
[1,2,3,4,5,5]
,结果将显示
[3,4,5]
,而不是
[4,5,5]

hughdbrown的解决方案有一个我注意到的错误。 如果列表中有类似的条目,那么结果将只显示其中一个条目。
例如,如果列表是
[1,2,3,4,5,5]
,结果将显示
[3,4,5]
,而不是
[4,5,5]

谢谢,你能告诉我我的实现哪里错了吗?@Naren,你的问题在
范围(0,k):newList.append(list[I])
谢谢,你能告诉我我的实现哪里错了吗?@Naren,你的问题在
中,因为I在范围(0,k):newList.append(list[I])
这可能是正确的答案,但你能让它更完整一点吗?目前它的质量不是很高。这可能是正确的答案,但你能让它更完整一点吗?目前的质量不是很高。