如何检查字符串是否为 pangram? [英] How to check if string is a pangram?
问题描述
我想创建一个函数,该函数将一个字符串作为输入并检查该字符串是否为 pangram(pangram 是一段包含字母表中每个字母的文本).
我编写了以下代码,该代码有效,但我正在寻找替代方法,希望是一种更短的方法.
导入字符串def is_pangram (gram):克 = 克.较低()gram_list_old = sorted([c for c in gram if c != ' '])gram_list = []对于 gram_list_old 中的 c:如果 c 不在 gram_list 中:gram_list.append(c)如果 gram_list == list(string.ascii_lowercase): 返回 True否则:返回假
我觉得这个问题可能违反了本网站的规则,但希望不是.我只是很好奇,并希望看到其他方法来做到这一点.
is_pangram = lambda s: not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower())>>>is_pangram('abc')错误的>>>is_pangram('敏捷的棕色狐狸跳过懒狗')真的>>>is_pangram('敏捷的棕色狐狸会跳过懒狗吗?')真的>>>is_pangram('大寒鸦喜欢我的石英狮身人面像吗?')真的
<块引用>
测试字符串 s
是一个全字母组合,如果我们从字母开始,删除测试字符串中找到的每个字母,然后删除所有字母.
说明
'lambda' 的使用是一种创建函数的方式,所以它是一行相当于写一个 def
像:
def is_pangram(s):return not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower())
set()
创建一个不能有任何重复的数据结构,这里:
- 第一组是(英文)字母,小写
- 第二组是测试字符串中的字符,也是小写的.并且所有重复项也都消失了.
减去诸如 set(..) - set(..)
之类的东西,返回第一个集合的内容,减去第二个集合的内容.set('abcde') - set('ace') == set('bd')
.
在这个 pangram 测试中:
- 我们从字母表中取出测试字符串中的字符
- 如果什么都没有,那么测试字符串包含了字母表中的所有字母,并且必须是全字母组合.
如果有剩余,那么测试字符串没有包含所有字母,所以它一定不是全字母组合.
测试字符串集中的任何空格、标点字符都从未出现在字母集中,因此它们无关紧要.
set(..) - set(..)
将返回一个空集合,或者一个包含内容的集合.如果我们将集合强制设置为 Python 中最简单的 True/False 值,则包含内容的容器为True",空容器为False".
所以我们使用 not
来检查还有没有剩余的东西?"通过将结果强制为 True/False 值,具体取决于是否有剩余.
not
也会改变 True -> False 和 False -> True.这在这里很有用,因为(字母用完)-> 一个空集,它是 False
,但我们希望 is_pangram
返回 True
案件.反之亦然,(字母表有一些剩余)-> 一组 True
的字母,但我们希望 is_pangram
返回 False
然后返回 True/False 结果.
is_pangram = lambda s: not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower())# 测试字符串`s`#is a pangram if# 字母# 减# 测试字符串字母# 没有剩菜
I want to create a function which takes a string as input and check whether the string is pangram or not (pangram is a piece of text which contains every letter of the alphabet).
I wrote the following code, which works, but I am looking for an alternative way to do it, hopefully a shorted way.
import string
def is_pangram (gram):
gram = gram.lower()
gram_list_old = sorted([c for c in gram if c != ' '])
gram_list = []
for c in gram_list_old:
if c not in gram_list:
gram_list.append(c)
if gram_list == list(string.ascii_lowercase): return True
else: return False
I feel like this question might be against the rules of this website but hopefully it isn't. I am just curious and would like to see alternative ways to do this.
is_pangram = lambda s: not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower())
>>> is_pangram('abc')
False
>>> is_pangram('the quick brown fox jumps over the lazy dog')
True
>>> is_pangram('Does the quick brown fox jump over the lazy dog?')
True
>>> is_pangram('Do big jackdaws love my sphinx of quartz?')
True
Test string
s
is a pangram if we start with the alphabet, remove every letter found in the test string, and all the alphabet letters get removed.
Explanation
The use of 'lambda' is a way of creating a function, so it's a one line equivalent to writing a def
like:
def is_pangram(s):
return not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower())
set()
creates a data structure which can't have any duplicates in it, and here:
- The first set is the (English) alphabet letters, in lowercase
- The second set is the characters from the test string, also in lowercase. And all the duplicates are gone as well.
Subtracting things like set(..) - set(..)
returns the contents of the first set, minus the contents of the second set. set('abcde') - set('ace') == set('bd')
.
In this pangram test:
- we take the characters in the test string away from the alphabet
- If there's nothing left, then the test string contained all the letters of the alphabet and must be a pangram.
If there's something leftover, then the test string did not contain all the alphabet letters, so it must not be a pangram.
any spaces, punctuation characters from the test string set were never in the alphabet set, so they don't matter.
set(..) - set(..)
will return an empty set, or a set with content. If we force sets into the simplest True/False values in Python, then containers with content are 'True' and empty containers are 'False'.
So we're using not
to check "is there anything leftover?" by forcing the result into a True/False value, depending on whether there's any leftovers or not.
not
also changes True -> False, and False -> True. Which is useful here, because (alphabet used up) -> an empty set which is False
, but we want is_pangram
to return True
in that case. And vice-versa, (alphabet has some leftovers) -> a set of letters which is True
, but we want is_pangram
to return False
for that.
Then return that True/False result.
is_pangram = lambda s: not set('abcdefghijklmnopqrstuvwxyz') - set(s.lower())
# Test string `s`
#is a pangram if
# the alphabet letters
# minus
# the test string letters
# has NO leftovers
这篇关于如何检查字符串是否为 pangram?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!