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

Robert Berman bermanrl at cfl.rr.com
Tue Nov 3 17:12:21 CET 2009


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 at cfl.rr.com>
> 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]]
> 
> 
> 
> 
> 
> 
> 
> 
>  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/884e489a/attachment.htm>


More information about the Tutor mailing list