简体   繁体   English

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

[英]TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

I'm having trouble with dealing with this error unsupported operand type error and I'm not sure what I'm doing wrong in this case.我在处理这个错误不受支持的操作数类型错误时遇到了麻烦,我不确定在这种情况下我做错了什么。 Any help would be appreciated!!任何帮助,将不胜感激!!

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

and printing closest_pair gives out:并打印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'

The error message is very explicit:错误信息非常明确:

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

It says that the program is trying to divide a tuple by an int.它表示该程序正试图将元组除以一个整数。 So, data[i - 1] + data[i] is a tuple, and that means that each of data[i - 1] and data[i] are tuples and not numbers as you may be expecting.因此, data[i - 1] + data[i]是一个元组,这意味着data[i - 1]data[i]都是元组,而不是您可能期望的数字。

Note that the error occurs inside the statistics.median function.请注意,该错误发生在statistics.median函数内部。 Check that you are passing arguments with the right type to this function.检查您是否将正确类型的参数传递给此函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:+不支持的操作数类型:“ int”和“ tuple” - TypeError: unsupported operand type(s) for +: 'int' and 'tuple' 类型错误:% 不支持的操作数类型:&#39;tuple&#39; 和 &#39;int&#39; - TypeError: unsupported operand type(s) for %: 'tuple' and 'int' 如何修复错误 - TypeError:不支持的操作数类型 - :&#39;tuple&#39;和&#39;int&#39; - How to fix error - TypeError: unsupported operand type(s) for -: 'tuple' and 'int' 获取 TypeError: 不支持的操作数类型 -: &#39;tuple&#39; 和 &#39;int&#39; - Getting TypeError: unsupported operand type(s) for -: 'tuple' and 'int' 如何修复 Python TypeError:不支持的操作数类型“int”和“tuple” - How to fix Python TypeError: unsupported operand type(s) 'int' and 'tuple' *args in recursion: TypeError: unsupported operand type(s) for +: 'int' and 'tuple' - *args in recursion: TypeError: unsupported operand type(s) for +: 'int' and 'tuple' TypeError:+ =不支持的操作数类型:“ int”和“ tuple”错误 - TypeError: unsupported operand type(s) for +=: 'int' and 'tuple' error TypeError:|:&#39;tuple&#39;和&#39;bool&#39;不支持的操作数类型 - TypeError: unsupported operand type(s) for |: 'tuple' and 'bool' TypeError:+的不支持的操作数类型:&#39;float&#39;和&#39;tuple&#39; - TypeError: unsupported operand type(s) for +: 'float' and 'tuple' 类型错误:% 不支持的操作数类型:&#39;tuple&#39; 和 &#39;str&#39; - TypeError: unsupported operand type(s) for %: 'tuple' and 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM