问题
问题描述:给定任意字符串,查找其中每个字符的最后一次出现,并按每个字符最后一次出现的先后顺序依次存入列表。例如对于字符串’abcda’的处理结果为[‘b’, ‘c’, ‘d’, ‘a’],而字符串’abcbda’的处理结果为[‘c’, ‘b’, ‘d’, ‘a’]。
测试字符串
1
s = 'aaaabcdawerasdfasdfwerngsnnvAAAweB3a'
解法
去重列表
1
2
3
4
5
6
result = []
for ch in s:
if ch in result:
result.remove(ch)
result.append(ch)
print(result)
正则
1
2
import re
print(re.findall(r'(\w)(?!.*\1)', s))
有序字典
1
2
from collections import OrderedDict
print(list(reversed(OrderedDict.fromkeys(reversed(s)))))