简体   繁体   English

在 python 中编写一个程序,其中不包含输入字符串中的任何重复字符。 示例:A 重复了一个

[英]write a program in python which does not contain any repeating character from input string . example: A is repeated of a

input is: This is An Example输入是: This is An Example

Output: This An Exmpl Output: This An Exmpl

li = input("Enter your Sting")
res = ''
for ch in li:
    if ch not in res :
        res = res + ch
        print(res, end=' ')
li = input("Enter your string")
res = ''
for ch in li.lower():
    if ch not in res:
        res = res + ch
print(res.title(), end='')

I am sure there will be a more elegant way to do this but for now here is a working version which you could refactor or reduce if needed我相信会有一种更优雅的方式来做到这一点,但现在这里有一个工作版本,如果需要你可以重构或减少

user_input = input("sentance: ")
seen = set()
filtered_words = []
for word in user_input.split():
    filtered_word = ""
    for char in word:
        if char.lower() in seen:
            continue
        filtered_word += char
        seen.add(char.lower())
    if filtered_word != "":
        filtered_words.append(filtered_word)
print(*filtered_words)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要编写一个 python 程序来打印重复次数为较大文本文件输入的数字的单词 - Need to write a python program to print words which are repeated number of times a number given as input from a text file of larger size 如果string不包含python中的任何字符串列表 - If string does not contain any of list of strings in python 在Python中,如何检查字符串是否不包含列表中的任何字符串? - In Python, how can I check that a string does not contain any string from a list? Python / Regex:如果一行确实包含某些特殊字符,则拆分字符串 - Python/Regex: Split string if a line does contain a certain special character Python - 如何检查我的字符串是否包含数组中的任何元素并获取重复值? - Python - How to check if my string contain any element in an array and get the repeated value? python 程序从输入写入 json - python program to write to json from input 编写一个程序,将一个字符作为输入(字符串长度为 1),输出是字母表中的下一个字符。 如果输入为“Z”,则输出为“A” - Write a program that takes a character as input (string length 1) the output is the next character in the alphabet. If input is 'Z', output is 'A' Python从程序重复输出 - Python repeated output from program python - 如何在python中编写代码,该代码是一个递归函数,返回一个字符串,其参数中的每个字符都重复? - How to write a code in python that is a recursive function that returns a string with each character in its argument repeated? 编写python代码,将字符串中所有重复的字符更改为“ @”,但该字符的首次出现除外 - Write python code that changes all the repeated characters in string to “@” except the first occurence of that character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM