Python 运行中位数的if/else内的全局范围

Python 运行中位数的if/else内的全局范围,python,variables,if-statement,global,scoping,Python,Variables,If Statement,Global,Scoping,我正在使用两个堆在Python中实现一个正在运行的中值算法。然而,即使我推动堆,堆的大小也不会增长 我怀疑这与if/else语句中的作用域有关。 我真的不明白如何解决这个问题 导入操作系统 进口numpy 导入功能工具 进口heapq 从heapq导入heapify、heappush、heappop @functools.total_排序 类反向比较(对象): 定义初始(自我,对象): self.obj=obj 定义(自身、其他): 返回isinstance(其他,反向比较)和self.obj=

我正在使用两个堆在Python中实现一个正在运行的中值算法。然而,即使我推动堆,堆的大小也不会增长

我怀疑这与
if/else
语句中的作用域有关。 我真的不明白如何解决这个问题

导入操作系统
进口numpy
导入功能工具
进口heapq
从heapq导入heapify、heappush、heappop
@functools.total_排序
类反向比较(对象):
定义初始(自我,对象):
self.obj=obj
定义(自身、其他):
返回isinstance(其他,反向比较)和self.obj==other.obj
定义(自我、其他):
返回isinstance(其他,反向比较)和self.obj>=other.obj
定义(自我):
返回str(self.obj)
定义报告(自我):
返回“%s(%r)”%(self.\uuuuuuu类\uuuuuuu名称\uuuuuuu,self.obj)
curMedian=0
leftHeap=map(ReverseCompare,[]
rightHeap=[]
heapq.heapify(右堆)
heapq.heapify(leftHeap)
def runningMed(n):
#导入全局变量
全球curmian
全局左堆
全局右堆
#第一次
如果(curMedian==0):
curmian=n
返回电流中值
#第二个+。。。时间
#打印“调试”
#打印右堆
#打印左堆
#heapq.heappush(leftHeap,3)
#heapq.heappush(右堆,3)
#打印右堆
打印“堆的长度”
打印透镜(右堆)
打印透镜(左堆)
如果(len(rightHeap)>len(leftHeap)+2:
打印“调试长右堆”
如果(n>=curMedian):
heapq.heappush(leftHeap,curmian)
curMedian=heapq.heappop(右堆)
heappop.heappush(右堆,n)
其他:
heapq.heappush(leftHeap,n)
elif(len(leftHeap)>len(rightHeap)+2:
打印“调试长”
如果(n):
heapq.heappush(右堆,n)
其他:
heapq.heappush(leftHeap,n)
#t转动中间带:
如果(len(leftHeap)=len(rightHeap)):
返回电流中值
elif(len(leftHeap)>len(rightHeap)):
返回值(heapq.heappop(leftHeap)+curMedian)/2
其他:
返回值(heapq.heapop(rightHeap)+curMedian)/2
如果名称=“\uuuuu main\uuuuuuuu”:
#TODO:必须更改输入/输出名称
inputFile=open('numbers.txt','r')
outputFile=open('output.txt','w')
对于inputFile中的行:
num=int(line.rstrip('\n'))
med=运行med(num)
outputFile.write(str(med)+'\n')
inputFile.close()
outputFile.close()

与范围无关。堆不会增长,因为您会在末尾立即弹出新添加的元素:

    return (heapq.heappop(leftHeap) + curMedian)/2
else:
    return (heapq.heappop(rightHeap) + curMedian)/2
只需查看“最大/最小”元素,不要将其弹出:

    return (leftHeap[0] + curMedian)/2
else:
    return (rightHeap[0] + curMedian)/2

我在评论中提到的我自己的版本:

from heapq import heappush, heappop

left, right = [], []
def runmed(n):
    global left, right
    if len(left) <= len(right):
        heappush(left, -n)
    else:
        heappush(right, n)
    if right and -left[0] > right[0]:
        heappush(left, -heappop(right))
        heappush(right, -heappop(left))
    if len(left) > len(right):
        return -left[0]
    return (right[0] - left[0]) / 2.0
输出:

38   is median of [38]
27.5 is median of [17, 38]
38   is median of [17, 38, 79]
27.5 is median of [4, 17, 38, 79]
17   is median of [4, 12, 17, 38, 79]
27.5 is median of [4, 12, 17, 38, 63, 79]
38   is median of [4, 12, 17, 38, 63, 69, 79]
35.0 is median of [4, 12, 17, 32, 38, 63, 69, 79]
38   is median of [4, 12, 17, 32, 38, 39, 63, 69, 79]
38.5 is median of [4, 12, 17, 32, 38, 39, 63, 69, 79, 82]
39   is median of [4, 12, 17, 32, 38, 39, 47, 63, 69, 79, 82]
38.5 is median of [4, 12, 17, 21, 32, 38, 39, 47, 63, 69, 79, 82]
38   is median of [4, 12, 17, 21, 25, 32, 38, 39, 47, 63, 69, 79, 82]
35.0 is median of [4, 12, 14, 17, 21, 25, 32, 38, 39, 47, 63, 69, 79, 82]
38   is median of [4, 12, 14, 17, 21, 25, 32, 38, 39, 47, 62, 63, 69, 79, 82]

Python全局范围可能有点挑剔。如果您将其封装在一个类中,其中每个实例都有自己的leftHeap、rightHeap和Curmian,那么生活可能会更轻松。

请注意,
leftHeap=map(ReverseCompare,[])
的结果与
leftHeap=[]
的结果相同。还要注意的是,
ReverseCompare
在代码中没有其他地方使用。代码中有很多注释掉的代码和多余的打印语句。还有一个未使用的
ReverseCompare
类。我建议把你的问题编辑成一个更易于管理的尺寸,省去不必要的代码。谢谢Stefan,这是最好的答案problem@JennyLian不客气。那么你能接受这个答案吗?很酷的问题,顺便说一句,我自己也编写了代码,只是为了好玩。我不确定我是否正确地接受了它。。。顺便说一句,你是如何完成最大堆部分的?(数字小于中位数)。我在python中找不到max heap,所以我必须对上面的所有数字应用-1。@JennyLian我现在将我的版本添加到我的答案中,以便您可以看到它。我通过否定这些数字来达到最大值,这似乎是最简单的。或者,您可以像最初使用ReverseCompare一样包装数字。还有一些未记录的函数,如
heapq.\u heapify\u max
,但是max只有一个组合的pushpop。
38   is median of [38]
27.5 is median of [17, 38]
38   is median of [17, 38, 79]
27.5 is median of [4, 17, 38, 79]
17   is median of [4, 12, 17, 38, 79]
27.5 is median of [4, 12, 17, 38, 63, 79]
38   is median of [4, 12, 17, 38, 63, 69, 79]
35.0 is median of [4, 12, 17, 32, 38, 63, 69, 79]
38   is median of [4, 12, 17, 32, 38, 39, 63, 69, 79]
38.5 is median of [4, 12, 17, 32, 38, 39, 63, 69, 79, 82]
39   is median of [4, 12, 17, 32, 38, 39, 47, 63, 69, 79, 82]
38.5 is median of [4, 12, 17, 21, 32, 38, 39, 47, 63, 69, 79, 82]
38   is median of [4, 12, 17, 21, 25, 32, 38, 39, 47, 63, 69, 79, 82]
35.0 is median of [4, 12, 14, 17, 21, 25, 32, 38, 39, 47, 63, 69, 79, 82]
38   is median of [4, 12, 14, 17, 21, 25, 32, 38, 39, 47, 62, 63, 69, 79, 82]