Python检测字符串中是否包含某字符集合中的字符

更新时间:2023-05-13 19:35:00 阅读: 评论:0

asianaPython检测字符串中是否包含某字符集合中的字符kill switch
⽬的
  检测字符串中是否包含某字符集合中的字符
⽅法
nationstar  最简洁的⽅法如下,清晰,通⽤,快速,适⽤于任何序列和容器
for all time
复制代码代码如下:
def containAny(q,at):
for c in q:
if c in at:
卸妆水怎么用return True
return Fal
第⼆种适⽤itertools模块来可以提⾼⼀点性能,本质上与前者是同种⽅法(不过此⽅法违背了Python的核⼼观点:简洁,清晰)
itertools.ifilter(predicate, iterable)的说明
Make an iterator that filters elements from iterable returning only tho for which the predicate is True. If predicate is None, return the items that are true.
例如:
ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9
复制代码代码如下:
survive用法
捷克语import itertools
def  containAny(q,at):
for item in itertools.ifilter(at.__contain__,q):
return True
return Fal
如果要检测两个字符串是否为包含关系,此时必须检查所有⼦项,最好适⽤t类型,其中t(at).difference(q)是指存在于at中⽽q没有的元素:
复制代码代码如下:
def  containAll(q,at):
bosom
return not t(at).difference(q)
ehcache例如下⾯这个例⼦:
复制代码代码如下:
In [4]: L1=[1,2,3,4]
In [5]: L2=[1,4,3,1]
In [6]: containAll(L1,L2)
清艳英语官方网站Out[6]: True
In [7]: containAll(L2,L1)
Out[7]: Fal
注意⼀下,t.symmetric_difference是指两个集合独有的元素复制代码代码如下:
In [9]: L2=[3,2,4,5]
In [10]: x=t(L1)
In [11]: x.symmetric_difference(L2)
Out[11]: t([1, 5])

本文发布于:2023-05-13 19:35:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/617498.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:代码   是否   本质   包含   字符
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图