0

I have an array that looks like the following: ["string", int , [] ] but when I try to increment the int Python gives me an error. (by the way I am new to python so this might be a simple question)

TypeError: 'int' object is not iterable

Here is my problematic part of the code:

for line in responseid_container:
    if line[0] is log.id_resp_address:
        ipFound = True
        #--- this line is where I get an exception ---
        responseid_container[1] += 1
        responseid_container[2].append = log.id_resp_address

(example for responseid_container = ['54.192.11.194', 1, [0]])

I tried to look for an answer in many places, such as: here,here,here, here , and many more... I hope it is not a duplicated answer but I did try and find if it is :-)

Here is my full code of the class if needed

from reader import fullLog

class counters:
    local_logs = []
    arrange_by_response_id = []

    def __init__(self , fulllog):
        self.local_logs = fulllog


    def arrangebyresonseid(self):
        responseid_container = [[" " , 0 , []]]
        ipFound = False
        counter = 0

        for log in self.local_logs.oopList:
            for line in responseid_container:
                if line[0] is log.id_resp_address:
                    ipFound = True
                    responseid_container[1] += 1
                    responseid_container[2].append = log.id_resp_address

                if not ipFound:
                    ipFound = False
                    responseid_container.append([log.id_resp_address , 1 , [counter]])

                counter += 1                    
Community
  • 1
  • 1
Tomer
  • 531
  • 7
  • 19
  • Appending to the container inside the loop you are iterating within it is bad practice and can cause bugs. Also, for what line do you get the error? – Doron Cohen Aug 11 '16 at 04:24
  • It's kind of hard to see what is generating the error, do you have a line number? But generally having an array of mixed types is a bad idea. For that I might recommend a tuple – Clement Aug 11 '16 at 04:26
  • Hey Doron, thanks on the quick response! what should I add to my code? an actual example of responseid_container? or an actual example of all variables? – Tomer Aug 11 '16 at 04:26
  • thanks clement! I will now study what is a tuple, it may solve the problem – Tomer Aug 11 '16 at 04:27
  • I add an example for responseid_container = ['54.192.11.194', 1, [0]] as you can see I am trying to increment the number 1 (second place in the array) – Tomer Aug 11 '16 at 04:41

1 Answers1

1

I checked your code you have problem at this statement:-

responseid_container[1] += 1

convert this line to:-

responseid_container[0][1] += 1

Check it carefully:-

responseid_container = [
                          0 => [
                            0 => " " , 1 => 0 , 2 => []
                          ]
                       ]
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30
  • Thanks Rakesh! Yes you are right, it was a silly mistake from my side. also thanks on the effort – Tomer Aug 11 '16 at 04:53