[英]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.