首页 > 解决方案 > 删除重复项时留下数字

问题描述

这是我删除字符串中重复字母的代码


word=input()

def dup_find(word):
    empty = ""  
    for character in word:
        if character not in empty or character == " ":
            empty = empty + character
    return empty

当输入为“6969 macaroni”时,输出为“69 macaroni”,但我希望单独留下数字,只删除重复的字母:6969 macaroni。

谢谢您的帮助。

标签: python

解决方案


这是正确的方法

text="Enter string:"+" "
string=input(text)

def rem_duplicates(string):
    x = ""  
    for letter in string:
        if letter.isdigit() or letter not in x or letter == ' ':
            x = x + letter
    return x

print(rem_duplicates(string))

推荐阅读