This is my original array: a = [0, 4, 7, 8, 1, 3, 5, 2, 6] and this is what it prints: [0, 4, 7, 0, 0, 0, 5, 0, 0] Answer As @’Lee Jun Wei’ it doesn’t look like your algo is right. But there may be a few other things to point out. These two lines look off. i and j are taking on the value of the elements of a not the index values. I think you mean The reason you get zeros is because min is initialized to 0. Note that it is zero not because
I’m trying to create a leaderboard for a game, where after the game has been played, the python script accesses a CSV file (not ordered) and prints out the top 5 people with the highest score. Python seems to do this fine with single-digit numbers, but I can’t get it to work with 2 digit numbers. Here is the code: and here is the output: (I just used placeholder names for now) and here is the original leaderboard file: How do I get it to order the numbers properly? Answer I suggest simply reading the file with file.readlines() and then
I am trying to sort out min page load time and max page load time from Apache access log file. After parsing the log file and sorting using sorted I am seeing weird sorting. The above code sorting thousandths and only then sorting hundreds: As you see 455 and 677 are at the end, but if you look separately at thousandths and hundreds sorting is going correctly. Can someone shed light into this please? BTW, if I don’t use map to tuple I am getting “TypeError: unhashable type: ‘list’” error for “Counter”, thus need to work with tuple. Sorting using
EDIT: I’m editing the whole post as it wasn’t as clear as I thougt. I have this dictionary (list of dictionaries within a dictionary) and I need to sort it by the values of “Position” and “Team”, meaning that I need to get all the values from those two keys and print them as I show below. I know how to get each value, but I don’t know how to get them and sort them. I tried this piece of code, but I still can’t get it to work. My actual dictionary is much bigger than this one, and I
I”m looking for a way to pickle all the variables with their values in a sorted order. I’m trying the method below: Expected result: Append value of va to totalList , then vb’s value..and so on. And then dump the totalList to a pickle file. I’m getting result like this: I want to get rid of the results (‘pickle’, ‘time’, ‘os’, ‘re’) and  and sort the result before appending to totalList or sort it before iteration. Answer Here’s a way to do what you want. You should ignore some variables you’re not interested in – specifically, imports and totalList.
A list of Point class objects is given. The Point class is as follows: The goal is to sort the list by the value attribute provided that if two objects have the same value attributes, the object whose is_start value is True should go before the object whose is_start value is False. An example of input: I’ve tried using array.sort(key=lambda x: x.value) and the output was: But it doesn’t satisfy the condition stated before (Point(3, True) should be before Point(3, False)). An example of desired output: What function to use to satisfy the stated condition? Answer Try this.
I have a data frame like this: As you can see, months are not in calendar order. So I created a second column to get the month number corresponding to each month (1-12). From there, how can I sort this data frame according to calendar months’ order? Answer Use sort_values to sort the df by a specific column’s values: If you want to sort by two columns, pass a list of column labels to sort_values with the column labels ordered according to sort priority. If you use df.sort_values([‘2’, ‘0’]), the result would be sorted by column 2 then column 0.