object is unsubscriptable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Avatar19
    New Member
    • Apr 2010
    • 43

    object is unsubscriptable

    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:
    Code:
    if i[j] == '1':
    and python is giving the error:

    Code:
    Traceback (most recent call last):
      File "D:\other py\lab11.py", line 7, in <module>
        if i[j] == '1':
    TypeError: 'int' object is unsubscriptable
    Code:
    inpt = raw_input().split()
    while inpt != 'END':
    	count = len(inpt)
    	for i in range(0,count):
    		outpt = str(i) + ':'
    		for j in range(0,count):
    			if i[j] == '1':
    				outpt = outpt + ' ' + j
    		inpt = raw_input().split()
    Any help would be greatly appreciated!
    Thanks
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • Avatar19
      New Member
      • Apr 2010
      • 43

      #3
      I tried inpt[j] because that seems logical to me but that didn't do it either?? Any suggestions?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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().spl it()

        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"....le t'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().spl it()

        Um...????
        raw_input()...w hat 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
          • loop back to get the next character
        • Loop back to get the next line


        In your code, as far as I can tell, you aren't looping through the lines retrieved from the file....
        I think this is your biggest problem in trying to figure out what's wrong.


        -Frinny

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          I would try to explain the following code, but I'm in a time crunch. Post back if you have questions.

          Oops. I just realized it's homework!

          Psuedocode:
          1. Open file
          2. Iterate on file, using variable line (for line in fileObj)
          3. break if line.strip() == "END"
          4. split line and initialize a list of lists with the split line as the first element
          5. Create a for loop using range(length of the split line - 1)
          6. Call fileObj next method
          7. Append the next line, stripped and split, to the list of lists
          8. Iterate on the list of lists using enumerate() (for j, item in enumerate(list_ of_lists))
          9. Print each line conditionally (HINT: ["%d" % (k) for k, s in enumerate(item) if s == '1'])
          Last edited by bvdet; Apr 29 '10, 09:51 PM. Reason: realized it's homework

          Comment

          • Avatar19
            New Member
            • Apr 2010
            • 43

            #6
            Hey Frinavale, i really appreciate your time even though you are not familiar with python.

            You are correct in what you have said, except with "while inpt != 'END':"
            I've come up with a much messier way of doing this that can illustrate the "while!=" loop.

            Code:
            Numbers = raw_input()
            ListNums = Numbers.split(" ")
            x = len(ListNums)
            Counter = 0
            while Numbers != "END":
            	output = str(Counter) + ":"
            	for j in range(0,x):
            		if ListNums[j] == "1":
            			output = output + " " + str(j) 
            			
            	print output
            	Counter+=1
            	if Counter == x:
            		Counter = 0
            	Numbers = raw_input()
            	ListNums = Numbers.split(" ")
            	x = len(ListNums)
            I know the other way can work, i understand the logic of what you have laid out in response an this new code does accomplish the desired effect in terms of the output but I am just very frustrated because the logic of the other program should be working like this one does but i just can't debug it, if you notice anything in this program that can help debug my previous code, I'd really appreciate it
            Thanks for the help thus far.

            Comment

            • Avatar19
              New Member
              • Apr 2010
              • 43

              #7
              Hey Bvdet this is homework but I have accomplished what the program desires I am just trying to figure out the best way of doing this so there is no conflict of interest in terms of you doing my work for me.
              Thanks for any help

              I do have one question if you could explain the last line of that pseudocode "(HINT: ["%d" % (k) for k, s in enumerate(item) if s == '1'])" what does it mean? is % taken as mod? I am unfamiliar with this. Thanks

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Avatar19,

                I appreciate your desire to do the work yourself. I wrote the short script in the format I described as though the data was to be read from a file. The same principles would apply if each line of data was entered via raw_input()or read from a list or was a continuous string. The HINT on the last line is a list comprehension that returns a list of the indices where the "1"s occur. The indices list can be joined into one string with " ".join(thelist) .

                Good luck.

                Comment

                • Avatar19
                  New Member
                  • Apr 2010
                  • 43

                  #9
                  Cool thanks I am going to try it tomorrow because it is currently 12:35 am here. Is it ok if i post you if i get stuck??

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    No problem.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Bvdet, do you know of any IDE (Integrated Development Environment) that can help Avatar19 step through their application to debug it?

                      I am a .NET developer and so I use Visual Studio for all of my development needs. I know that .NET has Iron Python ...and a quick google search turned up this codeplex project: IronPython Studio which looks like it can be installed without installing Visual Studio.

                      I have never used this IDE before so I can't say that I recommend it and I'm not sure it would be good for classic Python development (Python implementation that is not with Iron Python)....I don't know if it would be good for you or not. If you have the time, and if you're curious, you could check it out.

                      If you don't want to use something .NET related (which I can fully understand) ...google turned up a whole bunch of Python IDE's....

                      Eclipse (with PyDev) also looks like a good option (although I've never used it myself I've heard very good things about it)

                      -Frinny

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Hmm after snooping/reading a few links I'm getting the impression that most Python IDEs don't have debugging/step-through capibilities.

                        Apparently Phython developers like to use assertion statements to debug things.

                        Interesting...

                        -Frinny

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          Frinny,

                          I write all my code in PythonWin. I have used Idle also, but I strongly prefer PythonWin. It has a step through debugger, but I never use it. The tracebacks generally give me all the information I need. I am sure there are better IDEs, but I found something that works - if it ain't broke...

                          BV

                          Comment

                          Working...