Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 识别(不)出现在字符串中的字符_Python_String - Fatal编程技术网

Python 识别(不)出现在字符串中的字符

Python 识别(不)出现在字符串中的字符,python,string,Python,String,让我们使用以下字符串(在Python中): 现在,有没有一种有效的方法来查找没有出现在该字符串中的英文字母(甚至是特殊字符) 例如,在该字符串中,没有出现的字母是:c、j、k、l、p、q、v、x、z 谢谢。您可以使用以下设置: import string teststr = "There are times when your best efforts are not good enough" alphabet = set(string.ascii_lowercase) alphabet -

让我们使用以下字符串(在Python中):

现在,有没有一种有效的方法来查找没有出现在该字符串中的英文字母(甚至是特殊字符)

例如,在该字符串中,没有出现的字母是:
c、j、k、l、p、q、v、x、z

谢谢。

您可以使用以下设置:

import string

teststr = "There are times when your best efforts are not good enough"
alphabet = set(string.ascii_lowercase)
alphabet - set(teststr.lower())
# {'c', 'j', 'k', 'l', 'p', 'q', 'v', 'x', 'z'}
使用字符串模块将所有字母加载到一个集合中。集合也是唯一的,这对于A)快速和b)与您的问题直接相关非常有用


注意:我重命名了变量
str
,这也是您不希望覆盖的Python内置名称。

ASCII:对于字符串更改标志[ch]=1中的每个字符,使用大小字符集初始化为0的标志数组。Unicode:使用[ch]=>1的映射,因为它将是稀疏的。感谢您的回答。这很有效。但是,正如您可能已经注意到的,问题最初是针对Java而不是Python的。关于如何在Java中执行此操作的任何线索。
import string

teststr = "There are times when your best efforts are not good enough"
alphabet = set(string.ascii_lowercase)
alphabet - set(teststr.lower())
# {'c', 'j', 'k', 'l', 'p', 'q', 'v', 'x', 'z'}