'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. Advertisements

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

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

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.
Similar Threads
  1. David
    Replies:
    2
    Views:
    648
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    943
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    509
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    15,416
    Oliver Wong
    Aug 3, 2006
  5. maestro
    Replies:
    1
    Views:
    428
    Chris
    Aug 11, 2008
  6. newbie30

    *str++ and (*str)++

    newbie30, Aug 12, 2009, in forum: C Programming
    Replies:
    0
    Views:
    1,190
    newbie30
    Aug 12, 2009
  7. Ethan Furman
    Replies:
    4
    Views:
    467
    Roy Smith
    May 27, 2011
  8. Mark Janssen
    Replies:
    0
    Views:
    265
    Mark Janssen
    Apr 12, 2013
Loading...