Python练习题3.22输出大写英文字母

本题要求编写程序,顺序输出给定字符串中所出现过的大写英文字母,每个字母只输出一遍;若无大写英文字母则输出“Not Found”。

输入格式:

输入为一个以回车结束的字符串(少于80个字符)。

输出格式:

按照输入的顺序在一行中输出所出现过的大写英文字母,每个字母只输出一遍。若无大写英文字母则输出“Not Found”。

代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

s = str(input())
m = list(set(list(s)))

#set 去重之后会打乱顺序
m.sort(key = s.index)

value = 0
list = list()

for i in range(0,len(m)):
    if ord(m[i])>64 and ord(m[i])<91:
        list.append(m[i])
        value = value + 1

if value != 0 :
    print("".join(list))
else :
    print("Not Found")

这个程序相对来说还是比较简单的。

1、对列表进行去重,默认的去重会打乱顺序,使用m.sort(key = s.index)方法进行排序,按照源列表进行排序。

2、做判断新建一个列表list,将大写字母放进去。

3、输出大写字母


读书和健身总有一个在路上

原文地址:https://www.cnblogs.com/Renqy/p/12723592.html