2 回答

TA贡献1831条经验 获得超10个赞
您应该打印output
,不x
。
另外,请注意,您的方法将不会仅打印唯一字符,因为每个字母将在首次出现时添加。(在1
第一次检查时,它还没有放入output
...)

TA贡献1865条经验 获得超7个赞
您确实要打印output而不是x
>>> input = "1234a4321b1234c4321d"
>>> output = ""
>>> for x in input:
... if x not in output:
... output += x
...
>>> print output
1234abcd
>>>
但是那也不是你追求的。如果您希望您的输出在您最初的问题中提到过,那么请与您联系。
因此,也许您想检查数字
>>> output = ""
>>> for x in input:
... if x.isdigit() == False:
... output += x
...
>>> output
'abcd'
或者,如果您只想要独特的商品,则可能需要 set()
>>> output = ''.join(set((i for i in input)))
>>> output
'acbd1324'
最后,一种更简洁的方式来做您原来想做的事
>>> output = ''.join(set((i for i in input if i.isdigit() == False)))
>>> output
'acbd'
最后,@ RedBaron在对原始问题的评论中提供了一种甚至更简单的方法
output = ''.join(set(input))
添加回答
举报