Skip to content
Advertisement

Sort a list by value of a dict python

I need some help, how to use the algorithm selection sort to sort a list by the values of a dict. I wrote some code but I don’t know how continue, that the code work.

months = {"January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12}

L = ["March", "January", "December"]

e.g. sort the list by the values of the dict

def month(L):
    for i in months:
       minpos = i
       for j in range (months[i], len(L)):
           if months[L[j]] > months[minpos]:
             months[minpos] = months[L[j]]

       L[j], L[minpos] = L[minpos], L[i] 

return L

Advertisement

Answer

New Answer

  • Create class selection_sort which has get_sorted() method that will sort the list based on dictionary values using selection sort.
  • Here we compare two value and if first value is greater than second, than swap both the values. Repeat this step until entire list is sorted.

class selection_sort:
    """Sort the list based on dictionary value using selection sort."""
    def __init__(self,L):
        self.months = {"January": 1, "February": 2, "March": 3, "April": 4,
                   "May": 5,
                  "June": 6, "July": 7, "August": 8, "September": 9,
                  "October": 10, "November": 11, "December": 12}
        self.L = L

    def get_sorted(self):
        """ sorting list."""
        month_num =  [self.months[i] for i in self.L]
        flag = True  # set Flag is True to enter the loop.
        while flag:
            flag = False  # set Flag to False for no swaping done

            for i in range(len(month_num)-1):
                if month_num[i] > month_num[i+1]:
                    flag = True  # set Flag to False if no swaping is done
                    # swap
                    month_num[i], month_num[i+1] = month_num[i+1], month_num[i]

        L = [k for k, v in self.months.items() if v in month_num]
        print(L)
        return L

Test Case

  • Test which you have written is comparing L with Expected. It should compare actual with expected. self.assertEqual(L, expected)

  • Also, expected = ["March", "May", "December", "October", "September"] is incorrect. It should have been ["March", "May","September", "October", "December"]

import unittest
from selectionSort import selection_sort

# @unittest.skip("")
class TestInPlace(unittest.TestCase):
    def test_1(self):
        L = ["December", "September", "March", "October", "May"]
        obj = selection_sort(L)
        actual = obj.get_sorted()
        expected = ["March", "May","September", "October", "December"]
        self.assertEqual(actual, expected)

Old Answer

Try this,

#creating list L1
L1 = [months[L[i]] for i in range(len(L))]
print(L1)

#sorting list using algorithm selection sort
for i in range(1,len(L1)):
    j = 0
    if L1[j] > L1[i] :
        L1[j], L1[i] = L1[i] ,L1[j]
print(L1)

#Replacing values with key
sorted_list = [ k   for k,v  in months.items() for i in L1 if i == v]
print(sorted_list)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement