Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print letters not in string

Tags:

python

boolean

I'm trying to see if a word or sentence has each letter of the alphabet and I can't get it to print all the letters that isn't in the sentence/word.

alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'
,'u','v','w','x','y','z']
x = raw_input('')
counter  = 0
counter2 = 0
for i in range(len(x))
    counter += 1
    for o in range(26):
        counter2 += 1
        if alpha[counter2] not in x[counter]:

and I'm stuck there...

like image 988
user1730295 Avatar asked Sep 02 '25 11:09

user1730295


2 Answers

alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'
,'u','v','w','x','y','z'}
input_chars = set(raw_input())
print alphabet - input_chars

All we do is set difference between the set of alphabet characters and the set of characters in our input. Note that the difference operation can take as a second operand any iterable, so we don't even really have to turn our input into a set if we don't want to, although this will speed up the difference a small amount. Furthermore, there is a built-in string which gives us the ascii letters so we could do it like this:

import string
print set(string.ascii_lowercase) - raw_input()
like image 55
jlund3 Avatar answered Sep 04 '25 00:09

jlund3


using set difference:

import string
x=raw_input()
not_found=set(string.ascii_lowercase) - set("".join(x.split()))
print (list(not_found))

output:

>>> 
the quick brown fox
['a', 'd', 'g', 'j', 'm', 'l', 'p', 's', 'v', 'y', 'z']
like image 33
Ashwini Chaudhary Avatar answered Sep 04 '25 00:09

Ashwini Chaudhary