[英]How to check for duplicates in a string but not replace them?
Sample user input 用户输入示例
letters = input("Please input the scrambled letters in order: ")
Now we all know that there are only 26 letter in english and none of them repeat. 现在我们都知道英文只有26个字母,而且没有一个重复。 So how do I make sure that whatever the user inputs doesn't repeat(don't need to replace)? 那么如何确保用户输入的内容不重复(不需要替换)? I need to write an if statement with that algorithm. 我需要用该算法编写一个if语句。
if letters == nothing_duplicate:
do something
if len(letters) == len(set(letters)):
do something
If you want to check for duplicates and verify that they have input every letter: 如果要检查重复项并验证他们是否输入了每个字母:
import string
if set(letters.lower()) == set(string.lowercase):
# do something
To actually get the list of letters that are missing, you could do something like this: 要实际获取缺少的字母列表,您可以执行以下操作:
>>> set(string.lowercase).difference('abcdefghijklmnopqrst')
set(['u', 'w', 'v', 'y', 'x', 'z'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.