Skip to content

Tag: sorting

Sorting lists with multiple tie breakers

I have data in an array like so: I want to sort it by the amount of non-10 values, and I also want to sort it in ascending order for rows, and in descending order of number of 10s: Output: I want to implement a tie breaker system so that if the amount of 10s the same, it now orders

Sort one inside another

I have a class Person that contains names and hobbies. Then there’s a list that contains people. person1 = Person(“MarySmith”, [“dancing”, “biking”, “cooking”]) person2 = … people = [person1, person2,..] I need to return a list of people sorted alphabetically by their name, and also sort their list of hobbies alphabetically. This is what I have so far: This is

Can I sort two related arrays in parallel using python?

I have two NumPy arrays with the shape (74395, 1) storing float values where arr1[0] correlates to arr2[0] and so on. I would like to sort them together in ascending order by the values stored in the second array. As an example: wanted result: How could I do that in python? Answer Use numpy.argsort with numpy.take_along_axis: Output:

Combine two lists and sort them

Lets say I have two lists. I want to append list2 into list1 and then sort and add a new element at a specific index. I keep getting an error message saying: TypeError: ‘<‘ not supported between instances of ‘list’ and ‘int’ This is what I have tried: Answer Use sorted() if you want to print sorted list:

Sorting list based on dictionary keys in python

Is there a short way to sort a list based on the order of another dictionary keys? suppose I have: I want to sort the list to be [‘a’,’b’,’c’] based on the order of dic keys. Answer You can create a lookup of keys versus their insertion order in dic. To do so you can write: Using this you can

Kth Smallest Element in multiple sorted arrays

Let’s say we have two arrays: array1 = [2,3,6,7,9] array2 = [1,4,8,10] I understood how to find the kth element of two sorted arrays in log(min(m,n)) where m is the length of array1 and n is the length of array2 as follows: But I couldn’t figure out how to extend this to multiple sorted arrays case. For example, given 3