Quantcast
Viewing all articles
Browse latest Browse all 12

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

Create two separate list. One positive values one with negative values. sort the negative list, then concatenate them together:

>>> lst = [1, -2, 10, -12, -4, -5, 9, 2]>>> sorted([i for i in lst if i > 0]) + sorted([i for i in lst if i =< 0])[1, 2, 9, 10, -12, -5, -4, -2]>>> 

Viewing all articles
Browse latest Browse all 12

Trending Articles



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