我的解法,我发现想复杂了
from collections import Counter
while True:
try:
strs = input()
cnt = Counter(strs)
# cnt1 = sorted(cnt.items(),key=lambda x:x[1])
min_value = min(cnt.values())
res1 = []
for key,value in cnt.items():
if value==min_value:
res1.append(key)
res = []
for i in strs:
if i in res1:
continue
res.append(i)
print("".join(res))
except:
break
优化后的:
from collections import Counter
while True:
try:
strs = input()
cnt = Counter(strs)
# cnt1 = sorted(cnt.items(),key=lambda x:x[1])
min_value = min(cnt.values())
res1 = []
for key,value in cnt.items():
if value==min_value:
strs = strs.replace(key,"")
print(strs)
except:
break
这里要注意:
1、str.replace(old,new,count)
不是原地变换,需要一个新的变量来承接!
2、str.count(i) 可以得到字符(串)在str出现的次数
3、str.index(i) 可以得到字符(串)在str中的第一次出现的索引,如果不存在则报错