Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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 Quickselect不打印/返回数据透视_Python_Median_Quickselect - Fatal编程技术网

Python Quickselect不打印/返回数据透视

Python Quickselect不打印/返回数据透视,python,median,quickselect,Python,Median,Quickselect,下面是quickselect的代码 def quickSelect(lst, k): if len(lst) != 0: pivot = lst[(len(lst)) // 2] smallerList = [] for i in lst: if i < pivot: smallerList.append(i) largerList = [] for i in lst: if i > pi

下面是quickselect的代码

def quickSelect(lst, k):
if len(lst) != 0:
    pivot = lst[(len(lst)) // 2]
    smallerList = []
    for i in lst:
        if i < pivot:
            smallerList.append(i)
    largerList = []
    for i in lst:
        if i > pivot:
            largerList.append(i)
    count = len(lst) - len(smallerList) - len(largerList)
    m = len(smallerList)
    if k >= m and k < m + count:
        return pivot
        print(pivot)
    elif m > k:
        return quickSelect(smallerList, k)
    else:
        return quickSelect(largerList, k-m-count)
def快速选择(lst,k):
如果len(lst)!=0:
pivot=lst[(len(lst))//2]
smallerList=[]
对于lst中的i:
如果我想:
smallerList.append(i)
largerList=[]
对于lst中的i:
如果我>轴:
追加(一)
计数=len(lst)-len(小列表)-len(大列表)
m=len(小列表)
如果k>=m且kk:
返回quickSelect(小列表,k)
其他:
返回quickSelect(大列表,k-m-count)
我遇到的问题是,它运行时没有错误或任何东西,但是当它自己完成时,我希望它向pythonshell输出一些东西(在这个特定的例子中是列表的中间值),但我什么也没有得到。我做错什么了吗

至于我输入的lst和k

  • lst=[70120170200]
  • k=len(lst)//2
我也尝试过使用一些不同的k值,但没有效果

def quickSelect(lst, k):
    if len(lst) != 0:
        pivot = lst[(len(lst)) // 2]
        smallerList = []
        for i in lst:
            if i < pivot:
                smallerList.append(i)
        largerList = []
        for i in lst:
            if i > pivot:
                largerList.append(i)
        count = len(lst) - len(smallerList) - len(largerList)
        m = len(smallerList)
        if k >= m and k < m + count:
            return pivot
            print(pivot)
        elif m > k:
            return quickSelect(smallerList, k)
        else:
            return quickSelect(largerList, k-m-count)

lst = [70, 120, 170, 200]
k = len(lst) // 2

print(quickSelect(lst, k))

我唯一纠正的是缩进。

您可以使用列表理解优化此算法。还有,我认为你不需要数数

 def quickSelect(seq, k):
    # this part is the same as quick sort
    len_seq = len(seq)
    if len_seq < 2: return seq

    ipivot = len_seq // 2
    pivot = seq[ipivot]

    smallerList = [x for i,x in enumerate(seq) if x <= pivot and  i != ipivot]
    largerList = [x for i,x in enumerate(seq) if x > pivot and  i != ipivot]

    # here starts the different part
    m = len(smallerList)
    if k == m:
        return pivot
    elif k < m:
        return quickSelect(smallerList, k)
    else:
        return quickSelect(largerList, k-m-1)




if __name__ == '__main__':
    # Checking the Answer
    seq = [10, 60, 100, 50, 60, 75, 31, 50, 30, 20, 120, 170, 200]

    # we want the middle element
    k = len(seq) // 2

    # Note that this only work for odd arrays, since median in
    # even arrays is the mean of the two middle elements
    print(quickSelect(seq, k))
    import numpy
    print numpy.median(seq)
defquickselect(seq,k):
#此部分与快速排序相同
len_seq=len(seq)
如果len_seq<2:返回seq
ipivot=len_seq//2
pivot=序列[ipivot]
smallerList=[x代表i,x在枚举(seq)中,如果x轴和i!=ipivot]
#这里开始不同的部分
m=len(小列表)
如果k==m:
回程枢轴
elif k
可能只是格式不好,但缩进是错误的。您没有在函数定义之后缩进。我只是运行它(修复缩进),并使用您对lst和k的输入和quickSelect(lst,k)返回170。你能告诉我你是怎么称呼quickSelect的吗?你的期望是什么?尝试打印(quickSelect(lst,k)),以确保解释器打印结果。“打印(quickSelect(lst,k))”是我的问题。我要求在函数中打印。当我这样运行它时,它工作了。格式实际上是正确的,我只是把它复制粘贴到这里。谢谢:)您的代码非常优秀,但如果将
pivot
简单地定义为
seq[0]
,则代码会更简单;这样,您根本不需要使用
枚举
smallerList=[x代表x在seq[1:]中,如果x轴]
。我们可以这样做,但是我们需要小心轴的选择。例如,如果我们最终总是分成n-1个子数组,快速排序将有一个运行时O(n^2)而不是O(nlogn)。在中间选择枢轴并不阻止这种情况发生,但是它比在开始选择枢轴的可能性更小。
 def quickSelect(seq, k):
    # this part is the same as quick sort
    len_seq = len(seq)
    if len_seq < 2: return seq

    ipivot = len_seq // 2
    pivot = seq[ipivot]

    smallerList = [x for i,x in enumerate(seq) if x <= pivot and  i != ipivot]
    largerList = [x for i,x in enumerate(seq) if x > pivot and  i != ipivot]

    # here starts the different part
    m = len(smallerList)
    if k == m:
        return pivot
    elif k < m:
        return quickSelect(smallerList, k)
    else:
        return quickSelect(largerList, k-m-1)




if __name__ == '__main__':
    # Checking the Answer
    seq = [10, 60, 100, 50, 60, 75, 31, 50, 30, 20, 120, 170, 200]

    # we want the middle element
    k = len(seq) // 2

    # Note that this only work for odd arrays, since median in
    # even arrays is the mean of the two middle elements
    print(quickSelect(seq, k))
    import numpy
    print numpy.median(seq)