删除重复项时保留数字

  • 本文关键字:保留 数字 删除 python
  • 更新时间 :
  • 英文 :


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


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

当输入为"0"时;6969通心粉";输出为"0";69 macroni";但我希望数字保持不变,只删除重复的字母so:6969 macroni。

谢谢你的帮助。

这是的正确方式

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))

试试这个:

text = "Enter string: " # just do the space in the string
string = input(text)

def rem_duplicates(string):
x = ""
for letter in string:
if letter.isdigit() or letter not in x or letter == " ": # if the string is a number or letter is empty or letter not in x
x = x + letter
return x

print(rem_duplicates(string))

检查是否来自字符串isnumeric()的字母,然后设置条件。letter == ' '不是必需的,但您也可以设置。

text = "Enter string:"+" "
string = input(text)
def rem_duplicates(string):
x = ""
for letter in string:
if letter.isnumeric() or letter not in x or letter == ' ':
x = x + letter
return x

print(rem_duplicates(string))

最新更新