uu们这个可以用Python循环语句写出来么?

img

本来用一个replace()就能实现,非要用循环。代码如下:

str=input('请输入:')
chr=input('删除哪个字符? ')

res=''
for c in str:
    if c!=chr:
        res = res+c

print('原:{0}\n后:{1}\n删除了{2}个字符'.format(str,res,len(str)-len(res)))

思路:建立两个新的列表,一个存放不重复的元素,一个存放重复的,最后将不重复的按位置合并成字符串打印输出,重复的通过重复的列表在字符串中标查找有多少个


strs = input()
chongfu = []
unchongfu = []

for i in range(len(strs)):
    ischongfu = False
    for j in range(len(strs)):
        a=strs[i]
        b=strs[j]

        if  a== b and i!=j:
            ischongfu = True
            if strs[i] not in chongfu:
                chongfu.append(strs[i])
    if not  ischongfu:
        unchongfu.append(strs[i])
strsun=''.join(unchongfu)
print(strsun)
for  i  in chongfu:
    print(i,list(strs).count(i))