First time here? Check out the FAQ!

Ask Your Question
3

Call to sort() returns "None"

asked 11 years ago

Alan Chang gravatar image

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
4

answered 11 years ago

tmonteil gravatar image

updated 11 years ago

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.

Preview: (hide)
link

Comments

Thank you!

Alan Chang gravatar imageAlan Chang ( 11 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: 11 years ago

Seen: 2,136 times

Last updated: May 14 '13