擅长:python、mysql、java
<p>您可以执行以下操作:</p>
<pre><code>text = input("please enter some text ")
l, seen = [], set()
for c in text:
if c in seen and c != ' ':
l.append("@")
else:
l.append(c)
seen.add(c)
print(''.join(l))
</code></pre>
<p>您也可以直接在列表中检查以前的字符,但是集合具有更好的contains检查。类似地,可以直接组装字符串,但是list的<code>append</code>比不可变字符串的<code>+=</code>具有更好的性能</p>
<p>更简单,但性能更低,至少对于长输入而言:</p>
<pre><code>text = input("please enter some text ")
s = ''
for c in text:
s += c if c not in s or c == ' ' else '@'
print(s)
</code></pre>