Ask Your Question

# Call to sort() returns "None"

I am using SAGE to calculate the arguments of the eigenvalues of a randomly-generated matrix. Here is my code.

evals = Mat(CDF,10).random_element().eigenvalues()
eval_args = map(arg, evals)
print eval_args.sort()


For some reason, the call to .sort() does not work, and what is printed is "None." Why does this happen?

edit retag close merge delete

## 1 Answer

Sort by ยป oldest newest most voted

The method .sort() sorts the list eval_args on place (i mean it modifies the list eval_args), it does not return a sorted copy of eval_args (it returns nothing, hence your behaviour):

sage: evals = Mat(CDF,10).random_element().eigenvalues()
sage: eval_args = map(arg, evals)
sage: eval_args
[-2.88140694172,
2.66782234593,
-0.206748063577,
-0.663492557653,
-1.34361452031,
-1.67041594451,
-2.22990648845,
0.643653522447,
1.5894324166,
1.28058620418]
sage: eval_args.sort()
sage: eval_args
[-2.88140694172,
-2.22990648845,
-1.67041594451,
-1.34361452031,
-0.663492557653,
-0.206748063577,
0.643653522447,
1.28058620418,
1.5894324166,
2.66782234593]


As you can see, after calling eval_args.sort(), the list eval_args is sorted.

If you do not want your list eval_args to be modified and get a sorted copy of it, just use the function sorted():

sage: evals = Mat(CDF,10).random_element().eigenvalues()
sage: eval_args = map(arg, evals)
sage: eval_args
[1.77315647525,
2.73584430085,
0.678473279316,
1.0691046262,
-0.229457152027,
-2.26775012586,
-2.35863979204,
-1.30207117624,
-2.75762912811,
-0.789539732054]
sage: sorted(eval_args)
[-2.75762912811,
-2.35863979204,
-2.26775012586,
-1.30207117624,
-0.789539732054,
-0.229457152027,
0.678473279316,
1.0691046262,
1.77315647525,
2.73584430085]
sage: eval_args
[1.77315647525,
2.73584430085,
0.678473279316,
1.0691046262,
-0.229457152027,
-2.26775012586,
-2.35863979204,
-1.30207117624,
-2.75762912811,
-0.789539732054]


As you can see, the list eval_args was not modified by the function sorted(). Unfortunately, there is no .sorted() method for lists.

more

## Comments

Thank you!

( 2013-05-14 13:53:02 +0200 )edit

## Your Answer

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

Add Answer

## Stats

Asked: 2013-05-14 12:33:02 +0200

Seen: 516 times

Last updated: May 14 '13