对象是不可取消的 [英] object is unsubscriptable

查看:85
本文介绍了对象是不可取消的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的代码存在问题。我写的代码是取样本数据,例如:

0 0 0 0 1 0 1 0

0 0 1 0 0 1 0 1

1 0 0 0 1 0 0 1

1 0 0 0 1 1 0 1

1 1 0 0 0 1 1 0

0 1 1 0 1 0 0 1

1 1 1 0 0 1 0 0

0 1 0 1 1 0 1 0

0 1 1 1

0 0 1 0

1 0 0 1

0 1 0 0

0 1 1 0

1 0 1 0

0 0 0 0

1 0 0 0

0 1 1

1 0 0

1 0 0

结束


输出:

0:4 6

1:2 5 7

2:0 4 7

3:0 4 5 7

4:0 1 5 6

5:1 2 4 7

6:0 1 2 5

7: 1 3 4 6

0:1 2 3

1:2

2:0 3

3: 1

0:1 2

1:0 2

2:

3:0

0:1 2

1:0

2:0


代码的要点是采取任何数量的输入,但输入将始终是平方均值如果第一个输入的长度是5,那么前五行输入应被视为一个循环。很抱歉,如果我没有正确解释这一点,但快速扫描样本输入和输出将非常有意义。

我的代码正在崩溃:

Hi there, I am having a little problem with my code here. The code that I have written is to take sample data such as:
0 0 0 0 1 0 1 0
0 0 1 0 0 1 0 1
1 0 0 0 1 0 0 1
1 0 0 0 1 1 0 1
1 1 0 0 0 1 1 0
0 1 1 0 1 0 0 1
1 1 1 0 0 1 0 0
0 1 0 1 1 0 1 0
0 1 1 1
0 0 1 0
1 0 0 1
0 1 0 0
0 1 1 0
1 0 1 0
0 0 0 0
1 0 0 0
0 1 1
1 0 0
1 0 0
END

and output this:
0: 4 6
1: 2 5 7
2: 0 4 7
3: 0 4 5 7
4: 0 1 5 6
5: 1 2 4 7
6: 0 1 2 5
7: 1 3 4 6
0: 1 2 3
1: 2
2: 0 3
3: 1
0: 1 2
1: 0 2
2:
3: 0
0: 1 2
1: 0
2: 0

The point of the code is to take any amount of input, however the input will always be square meaning that if the length of the first input is 5 then the first five lines of input should be considered as a single cycle. Sorry if I am not explaining this properly but a quick scan at the sample input and output will make perfect sense of it.
My code is breaking down at the point:

展开 | 选择 | Wrap | 行号

推荐答案

查看您的代码我看到i不是数组......它是一个计数器变量。所以你不能拥有我[j](这没有意义)。


-Frinny
Looking at your code I see that "i" is not an array...it''s a counter variable. Therefore you cannot have i[j] (this doesn''t make sense).

-Frinny


我试过inpt [j]因为这对我来说似乎合乎逻辑,但那也不是吗?有什么建议吗?
I tried inpt[j] because that seems logical to me but that didn''t do it either?? Any suggestions?


我希望其他人接管这个帖子,因为我以前从未使用过python。我刚刚注意到你的标题没有描述你的问题而是相应地改变了它(请将来使用更具描述性的标题)。当我在阅读你的帖子找到一个反映你问题的合适标题的时候我[j]跳出来看我显然是错的。


让我们来看看你的代码和步骤通过这一行一次。如果我出错了,请随时纠正我,因为我根本不懂python。


第1行:

inpt = raw_input()。split ()


这里你正在采取...可能是一个文件的行并将其拆分为空格。

这意味着inpt是一个字符数组,预计是1或0.


第2行:

inpt!=''END'':


现在这对我来说没有意义。如何将数组与String进行比较?也许我的第一个假设是错误的...但对我来说,我认为你需要检查raw_input(从文件中检索的字符串),看它是否等于END......让我们继续我的首先假设,您可以更正或纠正您的代码...


第3行:

count = len(inpt)

很容易理解,你正在设置计数成为inpt数组的长度。


Line4:

我的范围(0,计数):

你开始从0循环到数组的长度。


第5行:

outpt = str(i)+'':''

您设置outpt到一个包含你在哪个迭代的字符串


第5行:

表示j在范围内(0,计数):

你出于某种原因正在开始另一个循环。


第6行:

如果我[j] ==''1'':

不确定你在这里要做什么....

您想要与1进行比较的数据是什么?

如果我们去的话我最初的假设是inpt包含一行中找到的字符数组......如果你想检查这个数组中的每个字符,那么你应该有inpt [j]


真的让你成为第5行的循环。你正在循环到计数再次......哪个有效,但......从4号线开始循环的重点是什么?


Line7:

outpt = outpt +' '''+ j

在这里,您要将数据附加到outpt中。变量。你附加了j实际上......我假设你试图指出该行中哪些索引包含1(你原来的回答中你并不是很清楚)。这条线很好。



Line8

inpt = raw_input()。split()


嗯...... ????

raw_input()......它是什么?

你为什么再把它拆分?



好​​好看看你的代码。

让我们看一下你在伪代码中真正要做的事情(描述需要做什么的文字) )
  • 从文件中获取所有行并将这些行存储到数组中以便进行处理
  • 遍历从文件中检索到的每一行
    • 获取行我们正在迭代期间将它拆分成一个字符数组
    • 循环遍历行中的每个字符并检查字符是否
      • 如果字符是1则显示索引所在的位置在数组中找到一个
      • 如果角色不是1,则不做任何事情
I hope someone else will take over this thread because I''ve never worked with python before...ever. I just noticed that your title was not descriptive of your problem and changed it accordingly (please, in the future, use a more descriptive title). While I was reading your post to find an appropriate title that reflected your problem i[j] jumped out at me as being obviously wrong.

Let''s look at your code and step through this one line at a time. Please feel free to correct me if I get something wrong because I do not know python at all.

Line 1:
inpt = raw_input().split()

Here you''re taking...maybe the line of a file and splitting it on spaces.
This means that "inpt" is an array of characters that are expected to be a 1 or 0.

Line2:
while inpt != ''END'':

Now this doesn''t make sense to me. How can you compare an array to a String? Maybe my first assumption was wrong...but to me I think you need to be checking raw_input (the string retrieved from the file) to see if it is equal to "END"....let''s continue on my first assumption, you can either correct me or correct your code later...

Line3:
count = len(inpt)
Easy enough to understand, you''re setting "count" to be the length of the inpt array.

Line4:
for i in range(0,count):
You are starting to loop from 0 to the length of the array.

Line5:
outpt = str(i) + '':''
You setting "outpt" to a string that contains which iteration you are on

Line5:
for j in range(0,count):
You are starting another loop for some reason.

Line6:
if i[j] == ''1'':
Not sure what you''re trying to do here....
What data are you trying to compare to "1"?
If we go by my original assumption that "inpt" contains an array of characters that are found on a line...and if you want to check each character in this array then, yes you should have inpt[j]

The thing that is really getting you is your loop on line 5. You''re looping to "count" again...which works but...what is the point of the loop starting on line 4 then?

Line7:
outpt = outpt + '' '' + j
Here you are appending data to the "outpt" variable. You are appending "j" actually...I''m assuming that you''re trying to indicate which indexes in the line contain 1 (you weren''t quite clear about this in your original response). This line is fine.


Line8
inpt = raw_input().split()

Um...????
raw_input()...what is it?
Why are you splitting it again?


OK enough of looking at your code.
Let''s look at what you''re actually trying to do in pseudo code (words describing what needs to be done)
  • Get all lines from the file and store these into an array for processing purposes
  • Loop through each line retrieved from the file
    • Get the line we are working with during this iteration and split it into an array of characters
    • Loop through each character in the line and check if the character
      • If the character is a 1 display the index where the one was found in the array
      • If the character is not a 1, do nothing


这篇关于对象是不可取消的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆