Page 1 of 1

Re: 'int' object does not support item assignment

Posted: Wed May 24, 2017 10:46 am
by scotty101
Not sure how you figured out that `s1 = 60,2` would create a 60x2 array, that command creates a tuple with the value (60, 2)

If you want to create an array of a pre-defined size where each element is preset to zero the following code will work

Code: Select all

w = 60
h = 2
array = [[0 for x in range(w)] for y in range(h)] 
(Python is a bit clunky here and for defining arrays people often use numpy https://docs.scipy.org/doc/numpy/refere ... umpy.zeros)

Re: 'int' object does not support item assignment

Posted: Wed May 24, 2017 12:56 pm
by hippy
scotty101 wrote:for defining arrays people often use numpy
Not elegant, but this is what I have tended to use ...

Code: Select all

def Dim( size, defaultValue=0 ):
  array = []
  while len(array) < size:
     array.append( defaultValue )
  return array

oneDimensionArray = Dim(5)

print( oneDimensionArray )
Edited : Only works for single dimension arrays; see later posts.

Re: 'int' object does not support item assignment

Posted: Wed May 24, 2017 5:41 pm
by jojopi
What is wrong with:

Code: Select all

array = [[0] * w] * h
(Strictly speaking, none of these examples creates an array, but rather a list of lists. Arrays are not really a thing in Python unless you import a module to implement them. Lists differ from conventional arrays in that the size is not fixed, and the elements are not constrained all to have the same type.)

Re: 'int' object does not support item assignment

Posted: Thu May 25, 2017 1:50 pm
by hippy
supra wrote:And I cannot figured out of index.
Perhaps post your full code. You can add diagnostic print statements to show what 'h' and 'w' are, what size of arrays you have created, and what the array index 'i' is when it fails.

My guess is you have 'w' and 'h' swapped round or there's some bug relating to those or forming the arrays in the code, that you have probably created a [2,60] array rather than a [60,2] array.

Re: 'int' object does not support item assignment

Posted: Thu May 25, 2017 2:06 pm
by Paeryn
jojopi wrote:What is wrong with:

Code: Select all

array = [[0] * w] * h
(Strictly speaking, none of these examples creates an array, but rather a list of lists. Arrays are not really a thing in Python unless you import a module to implement them. Lists differ from conventional arrays in that the size is not fixed, and the elements are not constrained all to have the same type.)
Because that will create one list of w items for the first row and then references to that first row for the other rows rather than new lists for each row.

Re: 'int' object does not support item assignment

Posted: Thu May 25, 2017 2:56 pm
by hippy
Paeryn wrote:
jojopi wrote:What is wrong with:...
Because that will create one list of w items for the first row and then references to that first row for the other rows rather than new lists for each row.
Oops; that also affects my own Dim() code given above. In my defence I have only ever used it for creating single dimension arrays, and failed to fully test the code. Thanks for highlighting the issue.

Re: [SOLVED]'int' object does not support item assignment

Posted: Thu May 25, 2017 4:55 pm
by Paeryn
Slightly better (especially if you use this method in Python2), use anonymous variables in the list comprehension if you don't actually use the values. It doesn't matter as much in Python3 as the variable is local to the list comprehension but in Python2 it isn't (not that _ is really an anonymous variable in Python, it's a valid variable name, it's just not likely to clash with any other variable name):-

Code: Select all

array = [[0 for _ in range(w)] for _ in range(h)]
In Python3:

Code: Select all

>>> x = 4
>>> array = [0 for x in range(3)]
>>> print(x)  ## x will still have the original value 4
4
Whereas the same in Python2:

Code: Select all

>>> x = 4
>>> array = [0 for x in range(3)]
>>> print x  ## Oops, x will now be 2 from the list comprehension
2
<Edit> The problem in Python2 is due to the list comprehension leaking the variable, not the iterator as I originally said.

Re: [SOLVED]'int' object does not support item assignment

Posted: Thu May 25, 2017 9:36 pm
by paddyg
You're not iterating your x values where you try to use them to create the x,y values after you create the empty lists with the comprehension. You might as well produce 'full' lists in the comprehension line. BUT...

As you don't seem to use array and array2 later you can just generate the points as you need them for drawing, a bit like

Code: Select all

    for i in range(0, 60):
        pt = [0, 0]
        angle = 6.0 * i * pi / 180
        for j, p in enumerate(pt):
            radius = cent[0] - 25 + 20 * j
            p = (int(cent[0] + radius * cos(angle)),
                 int(cent[1] + radius * sin(angle)))
        cv2.line(clk, pt[0], pt[1], (0, 255, 0), 2, cv2.LINE_AA)

Re: [SOLVED]'int' object does not support item assignment

Posted: Fri May 26, 2017 6:12 pm
by paddyg
OK, you can probably tell I didn't test it! It was more of an idea of how to approach the issue... pt is supposed to be an array of two points, each of which is tuple (x, y)

Code: Select all

    for i in range(0, 60):
        pt = [0, 0]
        angle = 6.0 * i * pi / 180
        for j in range(2):
            radius = cent[0] - 25 + 20 * j
            pt[j] = (int(cent[0] + radius * cos(angle)),
                 int(cent[1] + radius * sin(angle)))
        cv2.line(clk, pt[0], pt[1], (0, 255, 0), 2, cv2.LINE_AA)