简体   繁体   English

字符串的所有字符都属于一个集合吗?

[英]Do all chars of a string belong to a set?

Is there a straightforward way of finding whether all characters of a string belong to a given set of characters? 是否有一种直接的方法来查找字符串的所有字符是否属于给定的字符集? I have the following looping code that I feel can be shortened: 我有以下循环代码,我觉得可以缩短:

def allInSet(mystr, myset):
    result = True
    for char in mystr:
        result &= (char in myset)
    return result

Examples: 例子:

>>> allInSet("yyyow", set(['a','e','i','o','u','w','y']))
True
>>> allInSet("yowza", set(['a','e','i','o','u','w','y']))
False

also, list(mystr) in set(['a','b','c']) did not work. 另外, list(mystr) in set(['a','b','c'])不起作用。

>>> set("aeiou").issuperset("hello world")
False
>>> set("aeiou").issuperset("eoo")
True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取所有字符不在字符串python中 - Get all chars not in string python 如果所有字符串都需要长度或 null 终止符,那么高级语言如何仅用字符构造字符串? - If all strings either need a length or null terminator, how do high level languages just construct a string with chars? 一次替换字符串中的所有字符 - Replacing all chars in string in one run 在同一行中延迟打印字符串中的所有字符 - Print all chars in a String with delay in the same line 将字符串中的所有字符转换为python中的ascii hex - Converting all chars in a string to ascii hex in python 从字符串中删除所有出现的几个字符 - Remove all occurrences of several chars from a string 根据它们所属的类别集重新排列字符串中的单词 - Rearrange words in a string based on which category set they belong to 从二进制解码为字符串到集合中将字符串拆分为字符 Python - Decoding from Binary to string into a Set splits string into chars Python 用另一个字符替换字符串中所有出现的所有字符,而不会互相干扰 - Replace all occurrences of all chars within a string with another chars without interfering one with eachother 如何以特定的字符(实际上是一组字符)拆分字符串,但具有指定的长度 - How to split string at a specific char(set of chars, actually), but with specified length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM