Ask Your Question
0

Creating and sorting a 2D array

asked 2013-02-09 17:48:36 +0100

jquigley gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-02-09 20:33:53 +0100

ppurka gravatar image

Look at the python documentation, especially at the key function.

edit flag offensive delete link more

Comments

Sorry for the delayed response. We gave it a try, and it's definitely a much more natural way to sort. Thanks!

jquigley gravatar imagejquigley ( 2013-02-15 22:13:05 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-02-09 17:48:36 +0100

Seen: 828 times

Last updated: Feb 09 '13