求字符串中字符出现的位置:
x='abcd'
x.find('c') #返回2
求字符串中字符出现的次数:
x='abcd'
freq=x.count('c') #返回1
删除字符串中指定字符:
方法一,前后位置拼接:
x='abcd'
# 删除字符‘c’
label=x.find('c')
print(x[:label]+x[label+1:])
方法二,转为list,删除后再转回字符串:
x='abcd'
x_list=list(x)
x_list.remove('c')
print(''.join(x_list))
求list中指定元素的下标:
x=list('abcd')
x.index('c')
删除list中指定位置的元素:
x=list('abcd')
del x[2]
print(x)
示例:删除字符串中出现次数最少的字符:
实现删除字符串中出现次数最少的字符,若出现次数最少的字符有多个,则把出现次数最少的字符都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。
while True:
try:
x=input()
l=[x.count(i) for i in x]
x_=list(x)
lmin=min(l)
def removes(x,l):
try:
position=l.index(lmin)
x.remove(x[position])
l.remove(l[position])
removes(x,l)
except:
print(''.join(x))
removes(x_,l)
except EOFError:
break