3

How is this working? It checks if a string contains each character from a-z at least once?

import string

def ispangram(str1, alphabet=string.ascii_lowercase):  
    alphaset = set(alphabet)  
    return alphaset <= set(str1.lower()) 

This returns True for example:

ispangram("The quick brown fox jumps over the lazy dog")

I can only assume it is something to do with lexographical ordering as stated here, but still a bit confused.

Comparing two lists using the greater than or less than operator

When I read the link in this SO question:

https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types

It says:

Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode code point number to order individual characters. Some examples of comparisons between sequences of the same type.

But this isn't clear to me.

smackenzie
  • 2,880
  • 7
  • 46
  • 99

3 Answers3

6

It is a set operation, not list. Which is equivalent to,

alphaset.issubset(set(str1.lower()))

s <= t

s.issubset(t)

Test whether every element in s is in t.

See here: https://docs.python.org/2/library/sets.html

Edit: See here for the current version of Set. Though easier explanation is given in the old version (For comparisons).

Rockybilly
  • 2,938
  • 1
  • 13
  • 38
2

No. It's comparing two sets. So it's converting the input string to lower case and then using Python's set type to compare it with the set of lowercase letters.

This is very useful (and fast) technique for comparing two lists to see what members they have in common/difference.

TimGJ
  • 1,584
  • 2
  • 16
  • 32
0
def pangram(s):
alphabet = set('abcdefghijklmnopqrstuvwxyz')
s = s.replace(' ','').lower()
s= sorted(s)

count = {}

    #alphabet could be one sting within '' later sorted, but I just went straight to the point. 
    #After initializing my dictionary at null, we start the count    

for letter in s:
    if letter in count:
        count[letter] =[]
    else:
        count[letter] = 1

for letter in alphabet:
    if letter in count:
        count[letter] =[]
    else:
        count[letter] = 0
for letter in count:
    if count[letter]== 0:
        print (letter +' missing!')
print (count[letter]!= 0)
Humphrey
  • 1
  • 3