Home > OS >  How to sort a list in Python using two conditions?
How to sort a list in Python using two conditions?

Time:06-01

I have a list of list in Python that I want to sort based on two categories. The sorted list should have the lowest number in the last position and also the first element has to be greater than 400. I want to return the top 3 from the list.

x_list = [['422', '324', '733443'], ['342', '654', '674335'], 
         ['953', '456', '828854'], ['345', '886', '446678'], 
         ['3224', '533', '654333'], ['7545', '5567', '369990']]

It should return like this:

result = [['7545', '5567', '369990'], ['3224', '533', '654333'], ['422', '324', '733443']]

I have tried using the following code, but cannot combine the two conditions to sort the list:

result= []
result  = sorted(x_list, key=lambda x: int(x[-1]))[:3]

Can anyone plz help me to solve this? Thank you very much!!

CodePudding user response:

Consider this code:

filter_first = [el for el in x_list if int(el[0]) > 400]
sorted(filter_first, key=lambda el: el[-1])

This gives me

[['7545', '5567', '369990'], ['3224', '533', '654333'], ['422', '324', '733443'], ['953', '456', '828854']]

which is different from what you claimed you wanted the output to be, but I think it meets all your stated requirements. Can you explain the '953' sublist?

CodePudding user response:

I would first filter the list to only retain values where first element is greater than 400, and then sort the final result by ascending order based on last element, as you originally had it. For example:

x_list = [['422', '324', '733443'], ['342', '654', '674335'],
         ['953', '456', '828854'], ['345', '886', '446678'],
         ['3224', '533', '654333'], ['7545', '5567', '369990']]

x_list2 = [x for x in x_list if int(x[0]) > 400]
x_list2.sort(key=lambda x: int(x[2]))
x_list2 = x_list2[:3]

print(x_list2)
# [['7545', '5567', '369990'], ['3224', '533', '654333'], ['422', '324', '733443']]

Or you can also sort and filter in a single step as well:

x_list = [['422', '324', '733443'], ['342', '654', '674335'],
         ['953', '456', '828854'], ['345', '886', '446678'],
         ['3224', '533', '654333'], ['7545', '5567', '369990']]

x_list2 = sorted((x for x in x_list if int(x[0]) > 400), key=lambda x: int(x[2]))[:3]

print(x_list2)
# [['7545', '5567', '369990'], ['3224', '533', '654333'], ['422', '324', '733443']]

CodePudding user response:

Here's a customizable function:

def sort_filtered(list_, key=lambda x: x[2], filter_=lambda x: x[0] > 400, n=3):   
    # Turn values into floats so we can sort them
    float_list = [[float(x) for x in y] for y in list_]
    return [x for x in sorted(float_list, key=key) if filter_][:n]

CodePudding user response:

x_list = [['422', '324', '733443'], ['342', '654', '674335'],
         ['953', '456', '828854'], ['345', '886', '446678'],
         ['3224', '533', '654333'], ['7545', '5567', '369990']]

x_list = [a for a in x_list if float(a[0]) > 400]  # Filters out values <= 400
x_list.sort(key=lambda x : x[2])  # Sorts by the last element
x_list = x_list[:3]  # Only keeps the first 3 values
print(x_list)
  • Related