题目:给定一段字符串,求字符串中第一个出现一次的字母索引位置,没有则返回-1
def indexStr(st):
# strCount = {}
# for num in st:
# if num in strCount.keys():
# strCount[num] = strCount[num] + 1
# else:
# strCount[num] = 1
# if 1 not in list(strCount.values()):
# print(-1)
# else:
# keyOne = [k for k, v in strCount.items() if v == 1]
# co = st.find(keyOne[0])
# print(co)
for num in st:
if st.count(num) == 1:
return st.find(num)
return -1
- str.find(),str.index() 查找字母在字符串中第一次出现的索引位置,没有的换index报错,find返回-1
- str.count() 返回字母在字符串中的频次