ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 15 Feb 2013 22:13:05 +0100Creating and sorting a 2D arrayhttps://ask.sagemath.org/question/9791/creating-and-sorting-a-2d-array/How do you create a double-array in Sage (first component string, second component int)? And, how do you sort the double-array based on the value of the second component?
Right now, our code looks like:
G0 = []
pop = 0
while pop<1000:
G0.append(randomWord())
pop+=1
G0_fit = []
for i in range (0,1000):
G0_fit.append(FitFunc(G0[i]))
tempList = sorted(G0_fit)
cutoff = tempList[400]
inter = []
for i in range (0, 1000):
if G0_fit[i]<=cutoff:
inter.append(G0[i])
We want to avoid the "cutoff=tempList[400]" and following loop, and just sort both G0 and G0_fit in one step.Sat, 09 Feb 2013 17:48:36 +0100https://ask.sagemath.org/question/9791/creating-and-sorting-a-2d-array/Answer by ppurka for <p>How do you create a double-array in Sage (first component string, second component int)? And, how do you sort the double-array based on the value of the second component? </p>
<p>Right now, our code looks like:</p>
<pre><code>G0 = []
pop = 0
while pop<1000:
G0.append(randomWord())
pop+=1
G0_fit = []
for i in range (0,1000):
G0_fit.append(FitFunc(G0[i]))
tempList = sorted(G0_fit)
cutoff = tempList[400]
inter = []
for i in range (0, 1000):
if G0_fit[i]<=cutoff:
inter.append(G0[i])
</code></pre>
<p>We want to avoid the "cutoff=tempList[400]" and following loop, and just sort both G0 and G0_fit in one step.</p>
https://ask.sagemath.org/question/9791/creating-and-sorting-a-2d-array/?answer=14531#post-id-14531Look at the python documentation, especially at the [key function](http://docs.python.org/2/howto/sorting.html#key-functions).Sat, 09 Feb 2013 20:33:53 +0100https://ask.sagemath.org/question/9791/creating-and-sorting-a-2d-array/?answer=14531#post-id-14531Comment by jquigley for <p>Look at the python documentation, especially at the <a href="http://docs.python.org/2/howto/sorting.html#key-functions">key function</a>.</p>
https://ask.sagemath.org/question/9791/creating-and-sorting-a-2d-array/?comment=18225#post-id-18225Sorry for the delayed response. We gave it a try, and it's definitely a much more natural way to sort. Thanks!Fri, 15 Feb 2013 22:13:05 +0100https://ask.sagemath.org/question/9791/creating-and-sorting-a-2d-array/?comment=18225#post-id-18225