Python 每日一题:锻炼Python语法的运用,思维逻辑的锻炼,算法能力的培养。
题目:
返回给定字符串中第一个不重复的字符。
分析:
2、从左至右查找,返回第一个次数为0的元素。
实现:
def first_not_repeating_char(strings):
'''
查找给定字符串中第一个次数为1的字符
:param strings: str,查找字符串
:return: str,第一个次数为1的字符
'''
chars = []
times = []
for s in strings:
if s not in chars:
chars.append(s)
times.append(1)
else:
char_index = chars.index(s)
times[char_index] += 1
for t in times:
if t == 1:
times_index = times.index(t)
return chars[times_index]
return False
strings = input('请输入长字符串:')
print('结果:', first_not_repeating_char(strings))
结果:
请输入长字符串:abcdsabc
结果: d
【结语】以上整个程序的实现逻辑,还存在很多不全面的地方,欢迎各位大佬指点;如果觉得笔者不易,请给予点赞,给予我记录更多文章的动力!!