字符串是 Python 中最常用的数据类型。我们可以使用引号 ( ' 或 " ) 来创建字符串。
1、字符串拼接及not in
2、使用索引+not in
3、通过集合去重+列表
代码示例:
s='helloworldhelloworldadfekdiiikd'
#(1)字符串拼接及not in
new_s=''
for item in s:
if item not in new_s:
new_s+=item #拼接操作
print(new_s)
#(2)使用索引+not in
new_s2=''
for i in range(len(s)):
if s[i] not in new_s2:
new_s2+=s[i]
print(new_s2)
#(3)通过集合去重+列表排序
new_s3=set(s)
lst=list(new_s3)
lst.sort(key=s.index)
print(''.join(lst))