On Feb 23, 12:43 pm, Tim Chase wrote:
> I stumbled across this oddity and was hoping folks on the list
> might be able to provide a little understanding:
>
> # swap scalars
> >>> x,y = 1,2
> >>> x,y = y,x
> >>> x,y
> (2, 1)
>
> # swap lists
> >>> a,b = [1,2,3],[4,5,6]
> >>> a,b = b,a
> >>> a
andrew cooke wrote:
Delaney, Timothy (Tim) wrote:
Tim Chase wrote:
# swap list contents...not so much...
>>> m,n = [1,2,3],[4,5,6]
>>> m[:],n[:] = n,m
>>> m,n
([4, 5, 6], [4, 5, 6])
[...]
For these types of things, it's best to expand the code out. The
appropriate expansion of:
m,n = [
Tim Chase wrote:
I stumbled across this oddity and was hoping folks on the list might be
able to provide a little understanding:
# swap scalars
>>> x,y = 1,2
>>> x,y = y,x
>>> x,y
(2, 1)
# swap lists
>>> a,b = [1,2,3],[4,5,6]
>>> a,b = b,a
>>> a,b
([4, 5, 6], [1, 2, 3])
# swap list cont
En Mon, 23 Feb 2009 22:58:44 -0200, John Posner
escribió:
m,n = [1,2,3],[4,5,6]
m[:],n[:] = n,m
I believe this is an RTFM situation. In the Python 2.6.1 help topic
"Simple Statements" > "Assignment Statements", see this para:
Yes, the other relevant paragraph is:
"""An assign
>> m,n = [1,2,3],[4,5,6]
>> m[:],n[:] = n,m
I believe this is an RTFM situation. In the Python 2.6.1 help topic "Simple
Statements" > "Assignment Statements", see this para:
If the target is a slicing: The primary expression in the reference is
evaluated. It should yield a mutable seq
andrew cooke wrote:
> Delaney, Timothy (Tim) wrote:
>> Tim Chase wrote:
>>> # swap list contents...not so much...
>>> >>> m,n = [1,2,3],[4,5,6]
>>> >>> m[:],n[:] = n,m
>>> >>> m,n
>>> ([4, 5, 6], [4, 5, 6])
> [...]
>> For these types of things, it's best to expand the code out. The
>> appropriat
On Mon, Feb 23, 2009 at 10:43 AM, Tim Chase
wrote:
> # swap list contents...not so much...
m,n = [1,2,3],[4,5,6]
m[:],n[:] = n,m
m,n
> ([4, 5, 6], [4, 5, 6])
Pseudo-C-Python expansion:
#evaluate RHS. simply *take pointers* since the RHS is just plain variables
ptr_n = &n
ptr_m = &
On 2009-02-23 16:17, andrew cooke wrote:
Delaney, Timothy (Tim) wrote:
Tim Chase wrote:
# swap list contents...not so much...
>>> m,n = [1,2,3],[4,5,6]
>>> m[:],n[:] = n,m
>>> m,n
([4, 5, 6], [4, 5, 6])
[...]
For these types of things, it's best to expand the code out. The
appropriat
Delaney, Timothy (Tim) wrote:
> Tim Chase wrote:
>> # swap list contents...not so much...
>> >>> m,n = [1,2,3],[4,5,6]
>> >>> m[:],n[:] = n,m
>> >>> m,n
>> ([4, 5, 6], [4, 5, 6])
[...]
> For these types of things, it's best to expand the code out. The
> appropriate expansion of:
> m,n = [1,2
Tim Chase wrote:
> # swap list contents...not so much...
> >>> m,n = [1,2,3],[4,5,6]
> >>> m[:],n[:] = n,m
> >>> m,n
> ([4, 5, 6], [4, 5, 6])
>
>
> The first two work as expected but the 3rd seems to leak some
> internal abstraction. It seems to work if I force content-copying:
>
> >>> m[:
I stumbled across this oddity and was hoping folks on the list
might be able to provide a little understanding:
# swap scalars
>>> x,y = 1,2
>>> x,y = y,x
>>> x,y
(2, 1)
# swap lists
>>> a,b = [1,2,3],[4,5,6]
>>> a,b = b,a
>>> a,b
([4, 5, 6], [1, 2, 3])
# swap list contents...not so much...
>>
11 matches
Mail list logo