Quantcast
Channel: How do I sort a list with positives coming before negatives with values sorted respectively? - Stack Overflow
Browsing latest articles
Browse All 12 View Live
↧

Answer by Mohammad Mahjoub for How do I sort a list with positives coming...

*Here is another solution: *lst = [1, -2, 10, -12, -4, -5, 9, 2] # list of values.x = sorted(lst) # x : [-12, -5, -4, -2, 1, 2, 9, 10]k, m = [], [] # k : [1, 2, 9, 10] # M : [-12, -5, -4, -2]for i in...

View Article


Answer by RootTwo for How do I sort a list with positives coming before...

Sort the list in place twice like so:lst = [1, -2, 10, -12, -4, -5, 9, 2]lst.sort()lst.sort(key=int(0).__gt__) # key is True for items <= 0This takes advantage of the fact the python sort...

View Article


Answer by boardrider for How do I sort a list with positives coming before...

I don't know if it's the most Pythonic, and it certainly does not have any bells-and-whistles, but IMO it's a clear and understandable code:lst = [1, -2, 10, -12, -4, -5, 9, 2]pos = list()neg =...

View Article

Answer by John Smith for How do I sort a list with positives coming before...

Kudos to wmin for the logic of his solution (accepted answer) which is great. So for completeness, a similar answer to this but based on numpy, which is significantly faster for anything else other...

View Article

Answer by jpmc26 for How do I sort a list with positives coming before...

You could just sort by the negative of the element's inverse:from __future__ import divisionsorted(lst, key=lambda i: 0 if i == 0 else -1 / i)Taking the inverse switches the order of the magnitudes...

View Article


Answer by piRSquared for How do I sort a list with positives coming before...

import numpy as npl = np.array([1, -2, 10, -12, -4, -5, 9, 2])l[np.lexsort((l, l < 0))]array([ 1, 2, 9, 10, -12, -5, -4, -2])

View Article

Answer by John Smith for How do I sort a list with positives coming before...

Just comparing the different ways.Results:> Shuffle cost comparison smallshuffle_lst: 0.001181483967229724shuffle_ar: 0.014688121969811618> Shuffle cost comparison mediumshuffle_lst:...

View Article

Answer by Christian Dean for How do I sort a list with positives coming...

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...

View Article


Answer by John Smith for How do I sort a list with positives coming before...

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...

View Article


Answer by Moinuddin Quadri for How do I sort a list with positives coming...

Create two lists, one with positive value and another with the negative values and then sort the content of each list the way you like. For example:my_list = [1, -2, 10, -12, -4, -5, 9, 2]pos_list,...

View Article

Answer by wim for How do I sort a list with positives coming before negatives...

You could just use a regular sort, and then bisect the list at 0:>>> lst[1, -2, 10, -12, -4, -5, 9, 2]>>> from bisect import bisect>>> lst.sort()>>> i = bisect(lst,...

View Article

How do I sort a list with positives coming before negatives with values...

I have a list that contains a mixture of positive and negative numbers, as the followinglst = [1, -2, 10, -12, -4, -5, 9, 2]What I am trying to accomplish is to sort the list with the positive numbers...

View Article
Browsing latest articles
Browse All 12 View Live


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