简体   繁体   中英

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. 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 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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM