python - 检测一个字符串是否是 Python 中的 pangram

标签 python

这是如何运作的?它检查一个字符串是否至少包含一次从 a 到 z 的每个字符?

import string

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

例如这会返回 True:

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

我只能假设它与此处所述的词典顺序有关,但仍然有点困惑。

Comparing two lists using the greater than or less than operator

当我阅读这个 SO 问题中的链接时:

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

它说:

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.

但这对我来说并不清楚。

最佳答案

这是一个set操作,不是list。这相当于,

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

s <= t

s.issubset(t)

Test whether every element in s is in t.

看这里: https://docs.python.org/2/library/sets.html

编辑:查看此处了解 Set 的当前版本.虽然在旧版本中给出了更简单的解释(用于比较)。

关于python - 检测一个字符串是否是 Python 中的 pangram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47025311/

相关文章:

python - 如何使scrapy中的start_urls拾取另一个python函数生成的url?

python - 有没有其他类型的函数可以代替 turtle.goto?

python - tf.reset_default_graph 内存泄漏

python - django "in"子句是否要求列表有两个值?

JavaScript 相当于 Python 的 oauth2client 对 Google 提醒的 POST 请求

python - 将 Mat 参数从 Python 传递给 C++ 方法

Python 包导入冲突(Theano 和 openturns)

子树中的 python/mongodb 更新和结构

python - 连接两个变量名称以自动在列表 python 中查找内容

python - 为什么图像没有保存在 django 的媒体目录中?