Ask Your Question
0

Creating and sorting a 2D array

asked 12 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 12 years ago

ppurka gravatar image

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

Preview: (hide)
link

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 ( 12 years ago )

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: 12 years ago

Seen: 860 times

Last updated: Feb 09 '13