'str' object does not support item assignment

Discussion in 'Python' started by jimbo1qaz, Sep 23, 2012.

  1. jimbo1qaz

    jimbo1qaz Guest

    spots[y][x]=mark fails with a "'str' object does not support item assignment" error,even though:
    >>> a=[["a"]]
    >>> a[0][0]="b"

    and:
    >>> a=[["a"]]
    >>> a[0][0]=1000000

    both work.
    Spots is a nested list created as a copy of another list.
     
    jimbo1qaz, Sep 23, 2012
    #1
    1. Advertising

  2. jimbo1qaz

    jimbo1qaz Guest

    On Sunday, September 23, 2012 11:31:41 AM UTC-7, jimbo1qaz wrote:
    > spots[y][x]=mark fails with a "'str' object does not support item assignment" error,even though:
    >
    > >>> a=[["a"]]

    >
    > >>> a[0][0]="b"

    >
    > and:
    >
    > >>> a=[["a"]]

    >
    > >>> a[0][0]=1000000

    >
    > both work.
    >
    > Spots is a nested list created as a copy of another list.


    Yeah, it's a recursive function. Like it makes a difference.
     
    jimbo1qaz, Sep 23, 2012
    #2
    1. Advertising

  3. jimbo1qaz

    Ian Kelly Guest

    On Sun, Sep 23, 2012 at 12:31 PM, jimbo1qaz <> wrote:
    > spots[y][x]=mark fails with a "'str' object does not support item assignment" error,even though:
    >>>> a=[["a"]]
    >>>> a[0][0]="b"

    > and:
    >>>> a=[["a"]]
    >>>> a[0][0]=1000000

    > both work.
    > Spots is a nested list created as a copy of another list.


    There's not enough information to go on here to even speculate what
    might be causing the error, beyond "spots probably does not have the
    structure you think it does". Please post the code that you're
    actually running along with the full error traceback, and then maybe
    we can help you out. Also try printing the value of spots just before
    the assignment and see what it actually looks like.
     
    Ian Kelly, Sep 23, 2012
    #3
  4. jimbo1qaz

    MRAB Guest

    On 2012-09-23 19:31, jimbo1qaz wrote:
    > spots[y][x]=mark fails with a "'str' object does not support item assignment" error,even though:
    >>>> a=[["a"]]
    >>>> a[0][0]="b"

    > and:
    >>>> a=[["a"]]
    >>>> a[0][0]=1000000

    > both work.
    > Spots is a nested list created as a copy of another list.
    >

    The error suggests that spots[y] is actually a string. Printing
    repr(spots) and repr(spots[y]) should tell you whether it is.
     
    MRAB, Sep 23, 2012
    #4
  5. On 09/23/2012 07:31 PM, jimbo1qaz wrote:
    > spots[y][x]=mark fails with a "'str' object does not support item assignment" error,even though:
    >>>> a=[["a"]]
    >>>> a[0][0]="b"

    > and:
    >>>> a=[["a"]]
    >>>> a[0][0]=1000000

    > both work.
    > Spots is a nested list created as a copy of another list.


    But
    a = "a"
    a[0] = 'c'
    fails for the same reason, which is that strings in Python are immutable..
     
    Andrea Crotti, Sep 23, 2012
    #5
  6. jimbo1qaz

    jimbo1qaz Guest

    On Sunday, September 23, 2012 11:48:11 AM UTC-7, MRAB wrote:
    > On 2012-09-23 19:31, jimbo1qaz wrote:
    >
    > > spots[y][x]=mark fails with a "'str' object does not support item assignment" error,even though:

    >
    > >>>> a=[["a"]]

    >
    > >>>> a[0][0]="b"

    >
    > > and:

    >
    > >>>> a=[["a"]]

    >
    > >>>> a[0][0]=1000000

    >
    > > both work.

    >
    > > Spots is a nested list created as a copy of another list.

    >
    > >

    >
    > The error suggests that spots[y] is actually a string. Printing
    >
    > repr(spots) and repr(spots[y]) should tell you whether it is.


    ya, I'm an idiot. I accidentally made a string instead of an array.
     
    jimbo1qaz, Sep 23, 2012
    #6
  7. jimbo1qaz

    jimbo1qaz Guest

    On Sunday, September 23, 2012 11:48:11 AM UTC-7, MRAB wrote:
    > On 2012-09-23 19:31, jimbo1qaz wrote:
    >
    > > spots[y][x]=mark fails with a "'str' object does not support item assignment" error,even though:

    >
    > >>>> a=[["a"]]

    >
    > >>>> a[0][0]="b"

    >
    > > and:

    >
    > >>>> a=[["a"]]

    >
    > >>>> a[0][0]=1000000

    >
    > > both work.

    >
    > > Spots is a nested list created as a copy of another list.

    >
    > >

    >
    > The error suggests that spots[y] is actually a string. Printing
    >
    > repr(spots) and repr(spots[y]) should tell you whether it is.


    ya, I'm an idiot. I accidentally made a string instead of an array.
     
    jimbo1qaz, Sep 23, 2012
    #7
  8. On 23/09/2012 19:31, jimbo1qaz wrote:
    > spots[y][x]=mark fails with a "'str' object does not support item assignment" error,even though:
    >>>> a=[["a"]]
    >>>> a[0][0]="b"

    > and:
    >>>> a=[["a"]]
    >>>> a[0][0]=1000000

    > both work.
    > Spots is a nested list created as a copy of another list.
    >


    Looks to me as if there are three options.
    a) raise a bug report on the Python bug tracker at bugs.python.org.
    b) fix your code.

    As option b) is the likely route, the way forward is to put print
    statements in your code so you can see what data you have *AND* what
    type it is.

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Sep 23, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page