python教程6-3:排序

时间:2023-07-02 11:09:56

(a)、输入一串数字、并从大到小排列。

(b)、跟a一样,不过要用字典序从大到小排列。

python35 PaiXu_6_3.py

PaiXu_6_3.py

#coding=utf-8
import string

print ("Welcome to the PaiXu v1.0")
print ("Must be num.")
myInput = input('Are you ready?')
#myInput2 = {'host':'192.168.1.1', 'port':80}

def dict2list(dic:dict):
    keys = dic.keys()
    vals =
dic.values()
    lst = [(key, val) for
key, val in zip (keys, vals)]
    return lst

if myInput.isnumeric() == True:
    print
("(a)、输入一串数字、并从大到小排列。")
    myInput1 =
sorted(myInput)
    print
(myInput1[::-1])
    print
("(b)、跟a一样。不过要用字典序从大到小排列。")
    print
(myInput[::-1])
    #print
(sorted(dict2list(myInput2), key = lambda x:x[0],
reverse=True))
else:
    print (myInput + "is not
num.")