TypeError:/不支持的操作数类型:“元组”和“整数”

时间:2018-09-21 19:38:15

标签: python algorithm typeerror closest-points

我在处理此错误不受支持的操作数类型错误时遇到麻烦,并且不确定在这种情况下我在做什么错。任何帮助,将不胜感激!

def closest_pair(list):
if (len(list) <= 3):
    return min_distance(list)
else:
    left, right = split_into_two(list)
    left_min = closest_pair(left)
    right_min = closest_pair(right)
    if(left_min[2]>right_min[2]):
        return right_min
    else:
        return left_min

def split_into_two(list):
    med_val = statistics.median(list)
    med_x = med_val[0]
    left = []
    right = []
    for i in list:
        if (i[0]<med_x):
            left.append(i)
        else:
            right.append(i)
    return left, right

并打印最近的对将给出:

Traceback (most recent call last):
  File, line 109, in <module>
    print(closest_pair(text_file))
  File, line 61, in closest_pair
    left_min = closest_pair(left)
  File, line 62, in closest_pair
    right_min = closest_pair(right)
  File, line 60, in closest_pair
    left, right = split_into_two(list)
  File, line 44, in split_into_two
    med_val = statistics.median(list)
  File, line 358, in median
    return (data[i - 1] + data[i])/2
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

1 个答案:

答案 0 :(得分:0)

错误消息非常明确:

    return (data[i - 1] + data[i])/2
TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

它表示程序正在尝试将一个元组除以一个整数。因此,data[i - 1] + data[i]是一个元组,这意味着data[i - 1]data[i]中的每个都是元组,而不是您可能期望的数字。

请注意,该错误发生在statistics.median函数内部。检查是否将正确类型的参数传递给该函数。