Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

View: New views
7 Messages — Rating Filter:   Alert me  

Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

by Robert Berman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0

In [72]: l1[2][0] = 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/bermanrl/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

First question, is the error referring to the assignment (3) or the index [2][0]. I think it is the index but if that is the case why does l1[2][0] produce the value assigned to that location and not the same error message.

Second question, I do know that l1[2] = 3,1 will work. Does this mean I must know the value of both items in l1[2] before I change either value. I guess the correct question is how do I change or set the value of l1[0][1] when I specifically mean the second item of an element of a 2D array?

I have read numerous explanations of this problem thanks to Google; but no real explanation of setting of one element of the pair without setting the second element of the pair as well.

For whatever glimmers of clarity anyone can offer. I thank you.

Robert


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

by Wayne Werner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Nov 3, 2009 at 9:20 AM, Robert Berman <bermanrl@...> wrote:

In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0

This calls the element at index 2 which is:
(0,0) - a tuple, then calls element [0] from that tuple, which is 0

when you try to assign an item into a tuple, you get the same problem:

In [1]: x = (1,2,3)

In [2]: type(x)
Out[2]: <type 'tuple'>

In [3]: x[0]
Out[3]: 1

In [4]: x[0] = 0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/wayne/Desktop/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

And from your example:
In [6]: l1 = [(0,0)] *4

In [7]: type(l1[2])
Out[7]: <type 'tuple'>


In [72]: l1[2][0] = 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/bermanrl/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

First question, is the error referring to the assignment (3) or the index [2][0]. I think it is the index but if that is the case why does l1[2][0] produce the value assigned to that location and not the same error message.

Second question, I do know that l1[2] = 3,1 will work. Does this mean I must know the value of both items in l1[2] before I change either value. I guess the correct question is how do I change or set the value of l1[0][1] when I specifically mean the second item of an element of a 2D array?

When you use l1[2] = 3,1 it converts the right hand side to a tuple by implication - putting a comma between values:

In [8]: x = 3,1

 In [9]: type(x)
Out[9]: <type 'tuple'>

so when you say l1[2] = 3,1 you are saying "assign the tuple (3,1) to the list element at l1[2]" which is perfectly fine, because lists are mutable and tuples are not. 

 

I have read numerous explanations of this problem thanks to Google; but no real explanation of setting of one element of the pair without setting the second element of the pair as well.

For whatever glimmers of clarity anyone can offer. I thank you.

Hopefully this helps,
Wayne

p.s. If you want to be able to change individual elements, you can try this:
In [21]: l1 = [[0,0] for x in xrange(4)]

In [22]: l1
Out[22]: [[0, 0], [0, 0], [0, 0], [0, 0]]

In [23]: l1[2][0] = 3

In [24]: l1
Out[24]: [[0, 0], [0, 0], [3, 0], [0, 0]]


I don't know if there's a better way to express line 21, but you can't do it the other way or you'll just have the same list in your list 4 times:

In [10]: l1 = [[0,0]]*4

In [11]: l1
Out[11]: [[0, 0], [0, 0], [0, 0], [0, 0]]

In [12]: l1[2][0] = 3

In [13]: l1
Out[13]: [[3, 0], [3, 0], [3, 0], [3, 0]]




 

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

by Andre Engels :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Tue, Nov 3, 2009 at 4:20 PM, Robert Berman <bermanrl@...> wrote:

In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0

In [72]: l1[2][0] = 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/bermanrl/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

First question, is the error referring to the assignment (3) or the index [2][0]. I think it is the index but if that is the case why does l1[2][0] produce the value assigned to that location and not the same error message.

Second question, I do know that l1[2] = 3,1 will work. Does this mean I must know the value of both items in l1[2] before I change either value. I guess the correct question is how do I change or set the value of l1[0][1] when I specifically mean the second item of an element of a 2D array?

I have read numerous explanations of this problem thanks to Google; but no real explanation of setting of one element of the pair without setting the second element of the pair as well.

For whatever glimmers of clarity anyone can offer. I thank you.

Tuples are immutable types. Thus it is not possible to change one of the values of a tuple (or even of changing both of them). The only thing you can do, is create a new tuple, and put that in the same place in the list. In your example, when you do l1[2][0] = 3, you try to change the tuple l1[2], which is impossible.

To do what you want to do, you have to create a new array with the same second but different first value, and put that array in l1[2], that is:

l1[2] = (3, l1[2,1])


 

--
André Engels, andreengels@...


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

by Emile van Sebille :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11/3/2009 7:20 AM Robert Berman said...

>
> In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>
> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
>
> /home/bermanrl/<ipython console> in <module>()
>
> TypeError: 'tuple' object does not support item assignment

Start out slower...

a = (1,2)
a[1]=3

You can't change a tuple.  You can create a new tuple, or you can change
the content of an item help in a tuple, but you can't change the content
of the tuple itself.  Try the following then see if it helps answer your
questions...

b = [1]

a = (1,b)

b[0]=2

print a

a[1][0]=3

print a

Emile

>
> First question, is the error referring to the assignment (3) or the
> index [2][0]. I think it is the index but if that is the case why does
> l1[2][0] produce the value assigned to that location and not the same
> error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I
> must know the value of both items in l1[2] before I change either value.
> I guess the correct question is how do I change or set the value of
> l1[0][1] when I specifically mean the second item of an element of a 2D
> array?
>
> I have read numerous explanations of this problem thanks to Google; but
> no real explanation of setting of one element of the pair without
> setting the second element of the pair as well.
>
> For whatever glimmers of clarity anyone can offer. I thank you.
>
> Robert
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor@...
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

by Mark Tolonen-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


"Robert Berman" <bermanrl@...> wrote in message
news:1257261606.29483.23.camel@bermanrl-desktop...

>
> In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>
> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last)
>
> /home/bermanrl/<ipython console> in <module>()
>
> TypeError: 'tuple' object does not support item assignment
>
> First question, is the error referring to the assignment (3) or the
> index [2][0]. I think it is the index but if that is the case why does
> l1[2][0] produce the value assigned to that location and not the same
> error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I
> must know the value of both items in l1[2] before I change either value.
> I guess the correct question is how do I change or set the value of
> l1[0][1] when I specifically mean the second item of an element of a 2D
> array?
>
> I have read numerous explanations of this problem thanks to Google; but
> no real explanation of setting of one element of the pair without
> setting the second element of the pair as well.

Tuples are read-only, so you can't change just one element of a tuple:

    >>> x=0,0
    >>> x
    (0, 0)
    >>> x[0]=1
    Traceback (most recent call last):
      File "<interactive input>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment

You can, however, replace the whole thing, as you found:

    >>> x=1,1
    >>> x
    (1, 1)

To do what you want, you a list of lists, not a list of tuples, but there is
a gotcha.  This syntax:

    >>> L=[[0,0]]*4
    >>> L
    [[0, 0], [0, 0], [0, 0], [0, 0]]

Produces a list of the *same* list object, so modifying one modifies all:

    >>> L[2][0]=1
    >>> L
    [[1, 0], [1, 0], [1, 0], [1, 0]]

Use a list comprehension to create lists of lists, where each list is a
*new* list:

    >>> L = [[0,0] for i in range(4)]
    >>> L
    [[0, 0], [0, 0], [0, 0], [0, 0]]
    >>> L[2][0] = 1
    >>> L
    [[0, 0], [0, 0], [1, 0], [0, 0]]

-Mark


_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

by Robert Berman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you for your explanations and especially your clear examples of a phenomenon(when list elements are tuples) which takes a few moments of study to truly grasp.

Robert

On Tue, 2009-11-03 at 09:53 -0600, Wayne Werner wrote:
On Tue, Nov 3, 2009 at 9:20 AM, Robert Berman <bermanrl@...> wrote:


In [69]: l1=[(0,0)] * 4

In [70]: l1
Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]

In [71]: l1[2][0]
Out[71]: 0



This calls the element at index 2 which is:
(0,0) - a tuple, then calls element [0] from that tuple, which is 0


when you try to assign an item into a tuple, you get the same problem:


In [1]: x = (1,2,3)


In [2]: type(x)
Out[2]: <type 'tuple'>


In [3]: x[0]
Out[3]: 1


In [4]: x[0] = 0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)


/home/wayne/Desktop/<ipython console> in <module>()


TypeError: 'tuple' object does not support item assignment


And from your example:
In [6]: l1 = [(0,0)] *4


In [7]: type(l1[2])
Out[7]: <type 'tuple'>



In [72]: l1[2][0] = 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/bermanrl/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment

First question, is the error referring to the assignment (3) or the index [2][0]. I think it is the index but if that is the case why does l1[2][0] produce the value assigned to that location and not the same error message.

Second question, I do know that l1[2] = 3,1 will work. Does this mean I must know the value of both items in l1[2] before I change either value. I guess the correct question is how do I change or set the value of l1[0][1] when I specifically mean the second item of an element of a 2D array?



When you use l1[2] = 3,1 it converts the right hand side to a tuple by implication - putting a comma between values:


In [8]: x = 3,1


 In [9]: type(x)
Out[9]: <type 'tuple'>


so when you say l1[2] = 3,1 you are saying "assign the tuple (3,1) to the list element at l1[2]" which is perfectly fine, because lists are mutable and tuples are not. 


 

I have read numerous explanations of this problem thanks to Google; but no real explanation of setting of one element of the pair without setting the second element of the pair as well.

For whatever glimmers of clarity anyone can offer. I thank you.


Hopefully this helps,
Wayne


p.s. If you want to be able to change individual elements, you can try this:
In [21]: l1 = [[0,0] for x in xrange(4)]


In [22]: l1
Out[22]: [[0, 0], [0, 0], [0, 0], [0, 0]]


In [23]: l1[2][0] = 3


In [24]: l1
Out[24]: [[0, 0], [0, 0], [3, 0], [0, 0]]




I don't know if there's a better way to express line 21, but you can't do it the other way or you'll just have the same list in your list 4 times:


In [10]: l1 = [[0,0]]*4


In [11]: l1
Out[11]: [[0, 0], [0, 0], [0, 0], [0, 0]]


In [12]: l1[2][0] = 3


In [13]: l1
Out[13]: [[3, 0], [3, 0], [3, 0], [3, 0]]








 

_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Re: Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

by Bob Gailer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Robert Berman wrote:

>
> In [69]: l1=[(0,0)] * 4
>
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
>
> In [71]: l1[2][0]
> Out[71]: 0
>
> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last)
>
> /home/bermanrl/<ipython console> in <module>()
>
> TypeError: 'tuple' object does not support item assignment
>
> First question, is the error referring to the assignment (3) or the
> index [2][0]. I think it is the index but if that is the case why does
> l1[2][0] produce the value assigned to that location and not the same
> error message.
>
> Second question, I do know that l1[2] = 3,1 will work. Does this mean
> I must know the value of both items in l1[2] before I change either
> value. I guess the correct question is how do I change or set the
> value of l1[0][1] when I specifically mean the second item of an
> element of a 2D array?
>
> I have read numerous explanations of this problem thanks to Google;
> but no real explanation of setting of one element of the pair without
> setting the second element of the pair as well.
>
> For whatever glimmers of clarity anyone can offer. I thank you.

Tuples are immutable. Replace them with lists and voila. l1=[[0,0]] * 4

But also realize that you are creating a list with 4 copies of one
object [0,0]. Assigning to one changes all!


--
Bob Gailer
Chapel Hill NC
919-636-4239
_______________________________________________
Tutor maillist  -  Tutor@...
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor