Quantcast
Channel: How do I sort a list with positives coming before negatives with values sorted respectively? - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Answer by John Smith for How do I sort a list with positives coming before negatives with values sorted respectively?

$
0
0
import numpy as nplst = [1, -2, 10, -12, -4, -5, 9, 2]ar = np.array(lst)lst = list(np.concatenate([np.sort(ar[ar >= 0]), np.sort(ar[ar < 0], reverse = True)], axis = 0))print(lst)

And if you don't have to use a list but are happy with numpy arrays then you don't have to pay the costs of casting, i.e.

import numpy as npar = np.array([1, -2, 10, -12, -4, -5, 9, 2])ar = np.concatenate([np.sort(ar[ar >= 0]), np.sort(ar[ar < 0])], axis = 0)print(ar)

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>