Python 对象不可下标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19525996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object is not subscriptable
提问by Reubens
I'm having a bit of trouble with some python code (simple stuff). It's getting to the point where I'm hoping that if I click run enough times it might work...
我在使用一些 python 代码(简单的东西)时遇到了一些麻烦。它已经到了我希望如果我点击运行足够多的时间它可能会起作用的地步......
Here's the code:
这是代码:
Data = [1, 2, 3, 4, 5]
Frequency = [1, 2, 3, 3, 1]
def mode(data1, frequency1):
mode = [0]
count = 0
while count != len(frequency1):
if frequency1[count] > mode[0]:
mode = data1[count]
elif frequency1[count] == mode:
mode = [mode, data1[count]]
count +=1
return mode
mode = mode(Data, Frequency)
print(mode)
It returns :
它返回:
if Frequency[0] > mode[0]:
TypeError: 'int' object is not subscriptable
I had a look at another question & answer but it looked beyond me. Where am I going wrong!
我看了另一个问题和答案,但它看起来超出了我的范围。我哪里错了!
Edit: I know there are modules you can import to find the mean but I want to do it without importing anything.
编辑:我知道您可以导入一些模块来查找平均值,但我想在不导入任何内容的情况下进行。
Edit: entire code now posted. If I Don't make the mode variable a list it's fine until there are two modes.
编辑:现在发布了整个代码。如果我不将模式变量设为列表,那么在有两种模式之前都可以。
采纳答案by BlackVegetable
Check out your int(mode[0])
. You are basically saying, get the 0th element of... zero. Perhaps you just want to lose the subscript? Or change the type of mode?
检查您的int(mode[0])
. 您基本上是在说,获取...零的第 0 个元素。也许你只是想失去下标?或者改变模式的类型?