在字符串中查找第二个重复符号
Find 2nd repeating symbol in string
我有一个字符串,例如210132。如何找到字符串中的第二个重复符号(在本例中为 2)?
从第二个字符开始遍历字符串中的每个字符(因为第一个字符不能重复),跟踪当前位置。
在该位置之前的字符串部分中搜索该字符。
for pos, ch in enumerate(mystring[1:]):
if ch in mystring[:pos+1]:
print ch
我终于解决了我的任务,感谢@John Gordon 提出使用 enumerate 函数
的想法
print('Enter string')
while 1 > 0:
string = input()
if string == 'stop':
break
result = list()
for pos, ch in enumerate(string[1:]):
if ch in string[:pos+1]:
result.append(ch)
if len(result) < 2:
print('No second repeating character.Enter another string')
continue
print(result[1],'is the second repeating character')
我有一个字符串,例如210132。如何找到字符串中的第二个重复符号(在本例中为 2)?
从第二个字符开始遍历字符串中的每个字符(因为第一个字符不能重复),跟踪当前位置。
在该位置之前的字符串部分中搜索该字符。
for pos, ch in enumerate(mystring[1:]):
if ch in mystring[:pos+1]:
print ch
我终于解决了我的任务,感谢@John Gordon 提出使用 enumerate 函数
的想法print('Enter string')
while 1 > 0:
string = input()
if string == 'stop':
break
result = list()
for pos, ch in enumerate(string[1:]):
if ch in string[:pos+1]:
result.append(ch)
if len(result) < 2:
print('No second repeating character.Enter another string')
continue
print(result[1],'is the second repeating character')