简体   繁体   English

如何检查字符串中的重复但不替换它们?

[英]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.

相关问题 检查随机生成的列表中的重复项并替换它们 - Check for duplicates in a randomly generated list and replace them 有没有办法检查任何列表元素是否在字符串中并替换它们? - Is there a way to check if any of list elements are in a string and replace them? 如何在字符串中查找值,操作和替换它们 - How to find values in string, manipulate and replace them 如何计算一个字符串的实例并将它们替换为另一个字符串 + 当前计数器? - How to count instances of a string and replace them with another string + the current counter? 如何检查重复项之间的整个字符串? - How do I check the whole string between duplicates? python如何检查和替换字符串中的单词? - python how to check and replace words in string? 如何检测重复项,然后在其中交叉检查两列是否具有相似的值? - How do i detect duplicates and then among them cross check if two columns have similar values? 检查句子中是否存在某些字符串,并使用Python 3.6将其替换为另一个字符串 - Check if certain Strings are present in a Sentence and Replace them with another String using Python 3.6 如何用#替换文本文件中的重复项? - How to replace duplicates in a text file with a #? Python替换字符串中的3个随机字符,没有重复项 - Python replace 3 random characters in a string with no duplicates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM