python - 类型错误:不支持的操作数类型/: 'tuple' 和 'int'

标签 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

并打印 closest_pair 给出:

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'

最佳答案

错误信息非常明确:

    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 函数内部。检查您是否将正确类型的参数传递给此函数。

关于python - 类型错误:不支持的操作数类型/: 'tuple' 和 'int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52450139/

相关文章:

python - 从 pandas 中的对象中选择

python django 字典

Python - 在带标签的多维数组上应用函数

algorithm - 3x4 网格(行 x 列)上的 3 行游戏的 negamax 算法

java - 除大整数以获得精确值

javascript - Angular - 错误类型错误 : _co. timeIn 不是函数

python 线性回归: dense vs sparse

algorithm - 测试未排序的集合在线性时间内是否不相交。 (作业题)

python - 函数定义、类型错误

types - 尼姆 : How to constrain an existing type