ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 09 Feb 2012 09:06:54 -0600Swap coordinates of list elementshttp://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/Suppose I have list like
L = [(2,3), (4,5), (6,7)]
What's the easiest way to transform it to this list:
L'=[(3,2),(5,4),(7,5)]
i.e. swap the coordinates in each entry.Wed, 08 Feb 2012 03:41:42 -0600http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/Answer by DSM for <p>Suppose I have list like</p>
<pre><code>L = [(2,3), (4,5), (6,7)]
</code></pre>
<p>What's the easiest way to transform it to this list:</p>
<pre><code>L'=[(3,2),(5,4),(7,5)]
</code></pre>
<p>i.e. swap the coordinates in each entry.</p>
http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?answer=13261#post-id-13261There are several ways to do this. Which one I'd use would depend more upon the context, because I'd probably write the one which generalized most naturally.
First, explicitly (useful if we're sticking with 2D or if I might be interested in other transformations):
>>> L = [(2,3), (4,5), (6,7)]
>>> M = [(y,x) for x,y in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
or maybe using slice notation (seq[start:stop:step]):
>>> M = [v[::-1] for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
Or, using words instead:
>>> M = [tuple(reversed(v)) for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
>>> M = list(tuple(reversed(v)) for v in L)
>>> M
[(3, 2), (5, 4), (7, 6)]
And if the objects are really vectors, then maybe I'd use a rotation matrix. All depends, but I have to admit if there were only two things to swap I'd reach for the first method.Wed, 08 Feb 2012 04:02:41 -0600http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?answer=13261#post-id-13261Comment by kcrisman for <p>There are several ways to do this. Which one I'd use would depend more upon the context, because I'd probably write the one which generalized most naturally.</p>
<p>First, explicitly (useful if we're sticking with 2D or if I might be interested in other transformations):</p>
<pre><code>>>> L = [(2,3), (4,5), (6,7)]
>>> M = [(y,x) for x,y in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>or maybe using slice notation (seq[start:stop:step]):</p>
<pre><code>>>> M = [v[::-1] for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>Or, using words instead:</p>
<pre><code>>>> M = [tuple(reversed(v)) for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
>>> M = list(tuple(reversed(v)) for v in L)
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>And if the objects are really vectors, then maybe I'd use a rotation matrix. All depends, but I have to admit if there were only two things to swap I'd reach for the first method.</p>
http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20319#post-id-20319Sorry, I was referring to L. Adding a zero multiplied the timing by about 100 each time, it was quite impressive.Wed, 08 Feb 2012 15:45:22 -0600http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20319#post-id-20319Comment by kcrisman for <p>There are several ways to do this. Which one I'd use would depend more upon the context, because I'd probably write the one which generalized most naturally.</p>
<p>First, explicitly (useful if we're sticking with 2D or if I might be interested in other transformations):</p>
<pre><code>>>> L = [(2,3), (4,5), (6,7)]
>>> M = [(y,x) for x,y in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>or maybe using slice notation (seq[start:stop:step]):</p>
<pre><code>>>> M = [v[::-1] for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>Or, using words instead:</p>
<pre><code>>>> M = [tuple(reversed(v)) for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
>>> M = list(tuple(reversed(v)) for v in L)
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>And if the objects are really vectors, then maybe I'd use a rotation matrix. All depends, but I have to admit if there were only two things to swap I'd reach for the first method.</p>
http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20317#post-id-20317Strange - I could have sworn - but I get linear now too. Huh. That doesn't change the point that the first option still is the fastest :)Thu, 09 Feb 2012 09:06:54 -0600http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20317#post-id-20317Comment by kcrisman for <p>There are several ways to do this. Which one I'd use would depend more upon the context, because I'd probably write the one which generalized most naturally.</p>
<p>First, explicitly (useful if we're sticking with 2D or if I might be interested in other transformations):</p>
<pre><code>>>> L = [(2,3), (4,5), (6,7)]
>>> M = [(y,x) for x,y in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>or maybe using slice notation (seq[start:stop:step]):</p>
<pre><code>>>> M = [v[::-1] for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>Or, using words instead:</p>
<pre><code>>>> M = [tuple(reversed(v)) for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
>>> M = list(tuple(reversed(v)) for v in L)
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>And if the objects are really vectors, then maybe I'd use a rotation matrix. All depends, but I have to admit if there were only two things to swap I'd reach for the first method.</p>
http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20324#post-id-20324Umm, wouldn't you try to use the *fastest* one, all else being equal? Using `L = [(randint(0,10),randint(0,10)) for i in range(10000)]`, the first one wins hands down. A little (numerical, not # of ops) analysis shows that the first two options are slightly superlinear in the length of the list while the last is nearly exactly O(n^2). Anyway, go with the first one if you really only have pairs!Wed, 08 Feb 2012 08:02:19 -0600http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20324#post-id-20324Comment by DSM for <p>There are several ways to do this. Which one I'd use would depend more upon the context, because I'd probably write the one which generalized most naturally.</p>
<p>First, explicitly (useful if we're sticking with 2D or if I might be interested in other transformations):</p>
<pre><code>>>> L = [(2,3), (4,5), (6,7)]
>>> M = [(y,x) for x,y in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>or maybe using slice notation (seq[start:stop:step]):</p>
<pre><code>>>> M = [v[::-1] for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>Or, using words instead:</p>
<pre><code>>>> M = [tuple(reversed(v)) for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
>>> M = list(tuple(reversed(v)) for v in L)
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>And if the objects are really vectors, then maybe I'd use a rotation matrix. All depends, but I have to admit if there were only two things to swap I'd reach for the first method.</p>
http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20323#post-id-20323@kcrisman: well, sometimes I might want to use the most general one, all else being equal. Not everything's sliceable. (slicable?) I don't see O(n^2), though-- which list length are you talking about, L or v? Maybe I typed it wrong.Wed, 08 Feb 2012 08:37:31 -0600http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20323#post-id-20323Comment by DSM for <p>There are several ways to do this. Which one I'd use would depend more upon the context, because I'd probably write the one which generalized most naturally.</p>
<p>First, explicitly (useful if we're sticking with 2D or if I might be interested in other transformations):</p>
<pre><code>>>> L = [(2,3), (4,5), (6,7)]
>>> M = [(y,x) for x,y in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>or maybe using slice notation (seq[start:stop:step]):</p>
<pre><code>>>> M = [v[::-1] for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>Or, using words instead:</p>
<pre><code>>>> M = [tuple(reversed(v)) for v in L]
>>> M
[(3, 2), (5, 4), (7, 6)]
>>> M = list(tuple(reversed(v)) for v in L)
>>> M
[(3, 2), (5, 4), (7, 6)]
</code></pre>
<p>And if the objects are really vectors, then maybe I'd use a rotation matrix. All depends, but I have to admit if there were only two things to swap I'd reach for the first method.</p>
http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20318#post-id-20318I really don't seem to be able to reproduce this. timeit shows almost exactly linear growth for me. Could you double-check that? (Could be a memory reallocation thing? Hard to see where the nonlinearity would come from otherwise.)Thu, 09 Feb 2012 07:54:48 -0600http://ask.sagemath.org/question/8702/swap-coordinates-of-list-elements/?comment=20318#post-id-20318