ravenUSMC
ravenUSMC

Reputation: 517

Sorting a list of lists based on adding numbers in each individual list

I'm trying to sort a list of lists. The sort is based on adding numbers in each individual list. So I have a list that looks like this:

[['Turkey', '27', '73'], ['United States', '705', '1850'], ['Brazil', '26', '46'], ['Mexico', '35', '65'], ['Singapore', '17', '6']]

I need to sort this list based on adding 27 and 73 compared to 705 added to 1850 etc. As you can see above, the list is almost sorted since it started by looking something like this:

[['Brazil', '26', '46'], ['Mexico', '35', '65'], ['Singapore', '17', '6'], ['United States', '705', '1850'], ['Turkey', '27', '73']]

The code, which is a bubble sort, that I have looks like this:

def sort_graph_data(self, graph_data):
    for i in range(len(graph_data)):
        current_value_sum_first = self.get_list_total(graph_data[i])
            for j in range(len(graph_data) - 1):
                current_value_sum_second = self.get_list_total(graph_data[j + 1])
                if current_value_sum_first < current_value_sum_second:
                    graph_data[j + 1], graph_data[j] = graph_data[j], graph_data[j + 1]
    print(graph_data)

def get_list_total(self, value):
    current_value_sum = 0
    for v in value[1:]:
        v = int(v)
        current_value_sum = v + current_value_sum
    return current_value_sum

The reason that I have the method get_list_total is because each number is of the class <class 'numpy.str_'>. This could be fixed but I'm not as worried about it compared to getting the list sorted.

I will also say that each individual list could be longer than two numbers like it could be:

['United States', '705', '1850', '45', '12']

So the solution would have to account for that factor. I thought about doing something with a lambda function and sort but no luck.

What I have almost works but as you can see not quite.

Upvotes: 1

Views: 153

Answers (1)

mkrieger1
mkrieger1

Reputation: 23254

Instead of your custom bubble sort function, you can use the built-in sorted function which uses a much more efficient sorting algorithm and accepts another function to determine the sort criteria.

The get_list_total function should be perfect for that. Except that you can simplify it to:

def get_list_total(value):
    return sum(int(v) for v in value[1:])

at which point it is a matter of opinion whether a separate function should be written or a lambda expression lambda value: sum(int(v) for v in value[1:]) is used.

So you should simply be able to use this:

data = [['Turkey', '27', '73'], ['United States', '705', '1850'], ['Brazil', '26', '46'], ['Mexico', '35', '65'], ['Singapore', '17', '6']]
result = sorted(data, key=lambda value: sum(int(v) for v in value[1:]))
# or:    sorted(data, key=get_list_total)

Result:

[['Singapore', '17', '6'], ['Brazil', '26', '46'], ['Turkey', '27', '73'], ['Mexico', '35', '65'], ['United States', '705', '1850']]

Upvotes: 2

Related Questions