Python 二进制搜索给出TypeError:';int';对象不可调用

Python 二进制搜索给出TypeError:';int';对象不可调用,python,binary-search,Python,Binary Search,我正在尝试执行二进制搜索。我一直遇到一个错误: outcome = overlap (base, start_pos, end_pos) TypeError: 'int' object is not callable 该错误可在该代码的第58行找到。我已经在代码中标出了 def overlap (x, start_y, end_y): if x < start_y: return -2 elif x > end_y: retu

我正在尝试执行二进制搜索。我一直遇到一个错误:

    outcome = overlap (base, start_pos, end_pos)
TypeError: 'int' object is not callable
该错误可在该代码的第58行找到。我已经在代码中标出了

def overlap (x, start_y, end_y):
    if x < start_y:
        return -2
    elif x > end_y:
        return 2
    elif x >= start_y and x <= end_y:
        return 0

import csv

file1 = open ('path_to_file','rt', newline = '')
bait = csv.reader(file1, delimiter = '\t')

file2 = open ('path_to_file','rt', newline = '')
stress_33 = csv.reader(file2, delimiter = '\t')

file3 = open ('path_to_file','wt', newline ='')
output = csv.writer(file3, delimiter = '\t')

file4 = open ('path_to_file','wt', newline ='')
output_off = csv.writer(file4, delimiter = '\t')

files = file1, file2, file3, file4

bait_dict = {}

for line in bait:
    chromosome = line[0]
    start = int(line[1])
    end = int(line[2])
    location = start, end
    if chromosome not in bait_dict:
        bait_dict[chromosome] = []
        bait_dict[chromosome].append (location)
    elif chromosome in bait_dict:
        bait_dict[chromosome].append (location)

for item in bait_dict:
    bait_dict[item].sort(key = lambda i: i[0])

no_overlap = 0
overlap = 0

for i, line in enumerate(stress_33):
    if i == 0:
        output.writerow(line)
        output_off.writerow(line)
    elif i > 0:
        chrom_stress = line[1]
        base = int(line[2])
        if chrom_stress in bait_dict:
            for key, value in bait_dict.items():
                low = 0
                high = len(value) -1
                while low <= high:
                    mid = (low + high)//2
                    start_pos, end_pos = value[mid]
                    outcome = overlap (base, start_pos, end_pos) # TypeError: 'int' object is not callable
                    if outcome == 0:
                        output.writerow(line)
                    else:
                        if outcome == -2:
                            low = mid + 1
                        elif outcome == 2:
                            high = mid - 1
                no_overlap += 1
                output_off.writerow(line)    

print ('Number of ontarget is %d an number of off target is %d' %(overlap, no_overlap))


for file in files:
    file.close()
def重叠(x,开始y,结束y):
如果x结束y:
返回2
elif x>=开始y和x 0:
铬应力=线[1]
base=int(第[2]行)
如果饵料中的铬应力:
对于键,bait_dict.items()中的值:
低=0
高=透镜(值)-1

而low是因为您试图将
重叠
作为函数调用,而您将其声明为整数。我不确定您在第58行尝试做什么,因此很难建议如何修复它,但原因是,
overlap
根本不是一个函数


编辑:哦,我没有看到您在前面声明了一个名为
overlap
的函数。出现错误的原因是,在声明函数后,您将
overlap
重新声明为整数。只需为函数或值选择一个不同的名称,您就应该是金色的。

因为当您将其声明为整数时,您试图将其作为函数调用。我不确定您在第58行尝试做什么,因此很难建议如何修复它,但原因是,
overlap
根本不是一个函数


编辑:哦,我没有看到您在前面声明了一个名为
overlap
的函数。出现错误的原因是,在声明函数后,您将
overlap
重新声明为整数。只需为函数或值选择一个不同的名称,您就应该是金色的。

您不能同时拥有变量
overlap
和名为
overlap
的函数。在Python中,函数和值都是位于同一命名空间中的对象

顶部的函数称为
重叠,然后再向下绑定名称以包含一个整数:

overlap = 0
现在,您的函数对象不再是可寻址的(它已被清除,在该点之后不再存在)


重命名函数或变量。

不能同时拥有变量
overlap
和名为
overlap
的函数。在Python中,函数和值都是位于同一命名空间中的对象

顶部的函数称为重叠,然后再向下绑定名称以包含一个整数:

overlap = 0
现在,您的函数对象不再是可寻址的(它已被清除,在该点之后不再存在)


重命名函数或变量。

谢谢大家。太棒了!我真的很感激你发现了这个错误。我检查了几次代码,寻找类似这样的错误,但我找不到它。我猜当你们盯着一个代码看太久的时候就会发生这种情况。谢谢你们。太棒了!我真的很感激你发现了这个错误。我检查了几次代码,寻找类似这样的错误,但我找不到它。我猜当你盯着一个代码看太久的时候就会发生这种情况。