# 实现的功能如下:提示用户输入一个句子(英文,并且按enter键结尾), # 该程序将句子中的字母按ASCII码编码顺序重新排列,排列后的单词的长度要与原始句子中的长度相同, # 例如: 输入:who is your daddy,输出:add dh ioor suwyy s = input('please input a string:\n') # 先排序,去空格 tmp_s = list(filter(lambda c : not c.isspace(), sorted(s, reverse = True))) print(tmp_s) # 再在原始串空格位置上插入空格,其他位置为排序后的序列中的字符 new_s = ''.join(c if c.isspace() else tmp_s.pop() for c in s) print(new_s)
运行:
please input a string: who is your daddy ['y', 'y', 'w', 'u', 's', 'r', 'o', 'o', 'i', 'h', 'd', 'd', 'd', 'a'] add dh ioor suwyy