Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > 'str' object does not support item assignment

Reply
Thread Tools

'str' object does not support item assignment

 
 
jimbo1qaz
Guest
Posts: n/a
 
      09-23-2012
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.
 
Reply With Quote
 
 
 
 
jimbo1qaz
Guest
Posts: n/a
 
      09-23-2012
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.
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      09-23-2012
On Sun, Sep 23, 2012 at 12:31 PM, jimbo1qaz <(E-Mail Removed)> 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.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      09-23-2012
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.
 
Reply With Quote
 
Andrea Crotti
Guest
Posts: n/a
 
      09-23-2012
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..
 
Reply With Quote
 
jimbo1qaz
Guest
Posts: n/a
 
      09-23-2012
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.
 
Reply With Quote
 
jimbo1qaz
Guest
Posts: n/a
 
      09-23-2012
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.
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      09-23-2012
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
object indexing and item assignment King Python 1 11-13-2009 01:10 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
WebControl - CollectionEditor Problem. Changing id property of new added collection item causes not adding item to collection - Sergio ASP .Net Web Controls 0 05-29-2006 06:20 AM
Re: Unfortunate exception on dict item assignment (and why aren't slices hashable?) Erik Max Francis Python 10 08-02-2003 04:54 AM



Advertisments