#Python输出不重复的字符
#输入一个字符串,把最左边的10个不重复的字符(大小写算不同字符)挑选出来
#如果不重复的字符不到10个,则按实际数目输出
#输入格式:输入一个字符串s
#输出格式:输出一个字符串,包含字符串s最左边10个不重复的字符
#输入样例1:Hello world , hello python
#输出样例1:Helo wrd,h
def unique(s):
set1 = set([])
out = ""
for i in range(len(s)):
c = s[i]
if c not in set1:
out = out + c
set1.add(c)
return out[0:10]
def main():
string = "Hello world , hello python"
ret = unique(string)
print(ret)
if __name__ == '__main__':
main()
运行结果:

本文介绍如何使用Python编写一个函数,从输入字符串中提取并返回最左边的10个不重复字符。如果字符不足10个,将返回所有不重复字符。通过示例和代码展示如何处理输入如'Helloworld,hellopython'。
649

被折叠的 条评论
为什么被折叠?



