-1

I wrote code for two functions for a dictionary to be sorted either by key or by value in ascending order. I would like the user to be able to choose which sort function they want to be run by value or by key. My code for both is below.

d = {'b': 1, 'y': 12, 'c': 7, 'm': 2}
sort_d = sorted(d.items())
for i in sort_d:
    print(i[0], i[1])


d = {'b': 1, 'y': 12, 'c': 7, 'm': 2}
sort_d = sorted(d.items(), key=lambda x: x[1])
for i in sort_d:
    print(i[0], i[1])
martineau
  • 119,623
  • 25
  • 170
  • 301
sebz97
  • 3
  • 1

4 Answers4

2

No, there's no function that does exactly that per se, so you have write some code to do it yourself.

Applying the DRY principle to avoid repetitious code, you could use operator.itemgetter() to define a suitable key function for the sorted() function to use.

Here's what I mean:

from operator import itemgetter


while True:
    choice = input('sort by key or value (k or v)? ').lower()
    if choice in {'k', 'v'}:
        break
    else:
        print("Sorry, that's not a valid input, try again.\n")

keyfunc = itemgetter(0 if choice == 'k' else 1)

d = {'b': 1, 'y': 12, 'c': 7, 'm': 2}

print()
sort_d = sorted(d.items(), key=keyfunc)
for i in sort_d:
    print(i[0], i[1])

martineau
  • 119,623
  • 25
  • 170
  • 301
1

you can use input and if-elif-else to do it.

code:

def sort_by_key(d):
    sort_d = sorted(d.items())
    for i in sort_d:
        print(i[0], i[1])

def sort_by_value(d):
    sort_d = sorted(d.items(), key=lambda x: x[1])
    for i in sort_d:
        print(i[0], i[1])

d = {'b': 1, 'y': 12, 'c': 7, 'm': 2}
choose = input("Which sort function they want to be run? input 'key' or 'value'")
if choose == "key":
    sort_by_key(d)
elif choose == "value":
    sort_by_value(d)
else:
    print("invaild input")

result:

Which sort function they want to be run? input 'key' or 'value'key
b 1
c 7
m 2
y 12

Which sort function they want to be run? input 'key' or 'value'value
b 1
m 2
c 7
y 12

Which sort function they want to be run? input 'key' or 'value'xxxx
invaild input

EDIT:with a little improve to remove duplicate code

code:

def sort(d,keyfunc = None):
    sort_d = sorted(d.items(), key=keyfunc)
    for i in sort_d:
        print(i[0], i[1])

d = {'b': 1, 'y': 12, 'c': 7, 'm': 2}
operation_dic = {"key":None,"value":lambda x: x[1]}
while True:
    choose = input("Which sort function they want to be run? input 'key' or 'value'")
    if choose in operation_dic:
        sort(d,operation_dic[choose])
        break
    else:
        print("invaild input")
leaf_yakitori
  • 2,232
  • 1
  • 9
  • 21
0

You can define these functions and ask for user's input and then based on that input run the function:

answer = input("Type 1 to sort by values or 2 to sort by keys: ")
if answer == "1":
    sort_by_values(your_dict)
else:
    sort_by_key(your_dict)
Tikrong
  • 29
  • 4
0

This is were the use of control statements: if...elif...else:.

This is what you can try.

d = {'b': 1, 'y': 12, 'c': 7, 'm': 2}
x=input("How to sort the dicyionary? 'Key' or 'Value': ")
if x.lower() =="key":
    sort_d = sorted(d.items())
    for i in sort_d:
        print(i[0], i[1])
elif x.lower()=="value":  
    sort_d = sorted(d.items(), key=lambda x: x[1])
    for i in sort_d:
        print(i[0], i[1])
else: print("Invalid input. Please try again.")