scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: 'int' object does not support item assignment

Wed May 24, 2017 10:46 am

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)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 'int' object does not support item assignment

Wed May 24, 2017 12:56 pm

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.
Last edited by hippy on Thu May 25, 2017 2:58 pm, edited 4 times in total.

User avatar
jojopi
Posts: 3274
Joined: Tue Oct 11, 2011 8:38 pm

Re: 'int' object does not support item assignment

Wed May 24, 2017 5:41 pm

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.)

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 'int' object does not support item assignment

Thu May 25, 2017 1:50 pm

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.

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: 'int' object does not support item assignment

Thu May 25, 2017 2:06 pm

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.
She who travels light — forgot something.

hippy
Posts: 7911
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 'int' object does not support item assignment

Thu May 25, 2017 2:56 pm

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.

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

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

Thu May 25, 2017 4:55 pm

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.
She who travels light — forgot something.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

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

Thu May 25, 2017 9:36 pm

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)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

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

Fri May 26, 2017 6:12 pm

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)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”