Ordering a list sequentially but remove lower values that appear later

I have a list that looks like this:

[5000, 4000, 3500, 4200, 3300]

And I want to order this list from high to low, but also remove any values that have already been 'beat' by a lower value appearing earlier in the list. So above list should look like this:

[5000, 4000, 3500, 3300]

I have tried looping over the list and comparing it to every other value, but I am getting lost in for-loops. I think I am missing something very obvious.

Any help or other article is greatly appreciated!

'beat' by a lower value appearing earlier in the list means you can record the current minimum number, and compare to it. If larger than minimum number, remove it, If less than it, append to result and update the minimum.

Time complexity: O(n) Space complexity: O(1)

def trim_list_high_to_low(nums):
    min_num = float('inf')
    result = []
    for num in nums:
        if num <= min_num:
            result.append(num)
            min_num = num
    return result

output: [5000, 4000, 3500, 3300]

Retain or Revert Back to Original Sort Order in Excel, In this video I explain how to retain the original sort order of a range or Please try again later Duration: 3:03 Posted: 15 Mar 2019 The list doesn't even have to be sorted, the sufficient condition is that equal values are grouped together. Edit: I assumed that "preserving order" implies that the list is actually ordered. If this is not the case, then the solution from MizardX is the right one.

You can use a list comprehension:

d = [5000, 4000, 3500, 4200, 3300]
new_d = [a for i, a in enumerate(d) if not i or d[i-1] > a]

Output:

[5000, 4000, 3500, 3300]

Excel Sort (by Value, Color, Icon, Own List) & How to Unsort , Quickly learn how to use Excel's Sort feature to sort your data by value, color, icon and you to define the exact order you wish items to appear (more on this later.) If the newly inserted levels do not appear at the top of the list, but rather mixed in Our logic will be that any value in the lower third of the set will display a red� The LISTAGG function transforms non-null values from a group of rows into a list of values that are delimited by a configurable separator. LISTAGG can be used to de-normalize rows into a string of comma-separated values or other human-readable formats.

Use a snippet like this:

prev = 2**31
nlist = []
for i in your_list:
    if i < prev:
        nlist.append(i)
        prev = i

nlist result:

 [5000, 4000, 3500, 3300]

Filter for unique values or remove duplicate values, Filter for unique values, remove duplicate values, and conditionally format unique or duplicate values Article � Create a list of sequential dates Less. Windows Web.. In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. That's not really "preserving", because in a raw sense, you really did input bob first and fourth, not first and second.. There are no native datasets that I'm aware of that accomplish this, however, you can achieve this with a LinkedHashMap (preserves the order of the first time a key was entered) where the value is the count of the number of times the item was added:

Here's an approach that only compares each number with the last value of new_d:

d = [5000, 4000, 3500, 4200, 3300]
new_d = d[:1]
for item in d[1:]:
    if item < new_d[-1]:
        new_d.append(item)
print(new_d)

>>> [5000, 4000, 3500, 3300]

SQL ORDER BY, You can order data by multiple columns, in ascending or descending order. First, take a look at how the table is ordered by default: negative) numbers, with each successive row having a higher numerical value than the previous. the most recent years come first but orders top-ranks songs before lower-ranked songs: Turning it into a list is only required if you need a list to pass it to some method or if you want to add or remove more stuff later. It may also be useful if you have to enumerate the sequence multiple times because then you only have to evaluate the potentially expensive where condition once or if the result may change between two

Sorting and Removing Duplicates, Description of how to sort and remove duplicates in standard Excel and cell A7 and then filling down (by highlighting the range A7:A15 and pressing Ctrl-D). Next place the formula =SMALL(B$6:B$15,A4) in cell C6, highlight the range but instead the error value #NUM! will appear at the end of the sorted list in column C. Sorting a list or tuple is easy in Python! Since a tuple is basically like an array that is not modifiable, we'll treat it almost the same as a list. Sorting a Python List the Simple Way. Okay, so if you only want to sort a list of numbers, Python has a built in function that does all the hard work for you. Say we have a list of numbers:

11. Lists — How to Think Like a Computer Scientist: Learning with , Lists and strings — and other collections that maintain the order of their items We have already seen that we can assign list values to variables or pass lists as The bracket operator applied to a list can appear anywhere in an expression. As usual, the sublist selected by slice contains all the elements up to, but not� ordering_attr¶ – Name of the attribute that stores the object’s order in the relationship. ordering_func¶ – Optional. A function that maps the position in the Python list to a value to store in the ordering_attr. Values returned are usually (but need not be!) integers.

9. Lists — How to Think Like a Computer Scientist: Learning with , A list is an ordered set of values, where each value is identified by an index. We used it previously with strings, but it also works with lists and other sequences : The bracket operator applied to a list can appear anywhere in an expression. We can also remove elements from a list by assigning the empty list to them:. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation. List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index.

Comments
  • Hi, your question is unclear, with the example you gave I do not see any sorting. please provides a more meaningful example so we can help you
  • what happens if there are 2 equal values? Are they both stored or not?
  • all the answers so far only does filtering and no sorting. You need a better example if you want sorting as well or not.
  • Thanks a lot! I tried something like this but made errors I guess. Thanksss!