Python有一个字符串包含substring方法吗?
是的,但是Python有一个比较运算符,你应该使用它,因为语言意图使用它,而其他程序员则希望你使用它.该关键字是in,用作比较运算符:
>>> 'foo' in '**foo**'
原始问题要求的相反(补语)是not in:
>>> 'foo' not in '**foo**' # returns False
False
这在语义上是相同的,not 'foo' in '**foo**'但它在语言中作为可读性改进更加可读和明确地提供.
避免使用__contains__,find和index
正如所承诺的,这是contains方法:
str.__contains__('**foo**', 'foo')
回报True.您也可以从超级字符串的实例中调用此函数:
'**foo**'.__contains__('foo')
但不要.以下划线开头的方法在语义上被认为是私有的.使用它的唯一原因是扩展in和not in功能(例如,如果子类化str):
class NoisyString(str):
def __contains__(self, other):
print('testing if "{0}" in "{1}"'.format(other, self))
return super(NoisyString, self).__contains__(other)
ns = NoisyString('a string with a substring inside')
>>> 'substring' in ns
testing if "substring" in "a string with a substring inside"
另外,请避免使用以下字符串方法:
>>> '**foo**'.index('foo')
>>> '**foo**'.find('foo')
>>> '**oo**'.find('foo')
>>> '**oo**'.index('foo')
Traceback (most recent call last):
File "", line 1, in
'**oo**'.index('foo')
ValueError: substring not found
其他语言可能没有直接测试子字符串的方法,因此您必须使用这些类型的方法,但使用Python时,使用in比较运算符会更有效.
我们可以比较实现相同目标的各种方式.
import timeit
def in_(s, other):
return other in s
def contains(s, other):
return s.__contains__(other)
def find(s, other):
return s.find(other) != -1
def index(s, other):
s.index(other)
except ValueError:
return False
else:
return True
perf_dict = {
'in:True': min(timeit.repeat(lambda: in_('superstring', 'str'))),
'in:False': min(timeit.repeat(lambda: in_('superstring', 'not'))),
'__contains__:True': min(timeit.repeat(lambda: contains('superstring', 'str'))),
'__contains__:False': min(timeit.repeat(lambda: contains('superstring', 'not'))),
'find:True': min(timeit.repeat(lambda: find('superstring', 'str'))),
'find:False': min(timeit.repeat(lambda: find('superstring', 'not'))),
'index:True': min(timeit.repeat(lambda: index('superstring', 'str'))),
'index:False': min(timeit.repeat(lambda: index('superstring', 'not'))),
现在我们看到使用in比其他人快得多.更少的时间进行等效操作更好:
>>> perf_dict
{'in:True': 0.16450627865128808,
'in:False': 0.1609668098178645,
'__contains__:True': 0.24355481654697542,
'__contains__:False': 0.24382793854783813,
'find:True': 0.3067379407923454,
'find:False': 0.29860888058124146,
'index:True': 0.29647137792585454,
'index:False': 0.5502287584545229}
为什么要避免使用`str.index`和`str.find`?你怎么建议有人找到子串的索引而不是它是否存在?(或者你的意思是避免使用它们代替包含 - 所以不要使用`s.find(ss)!= -1`而不是s`中的`s?) (4认同)
确切地说,尽管通过优雅使用"re"模块可以更好地解决使用这些方法背后的意图.我还没有在我编写的任何代码中找到str.index或str.find的用法. (2认同)
Aaron Hall..159Python有一个字符串包含substring方法吗?是的,但是Python有一个比较运算符,你应该使用它,因为语言意图使用它,而其他程序员则希望你使用它.该关键字是in,用作比较运算符:>>> 'foo' in '**foo**'True原始问题要求的相反(补语)是not in:>>> 'foo' not in '**foo**'...
本文实例讲述了
python
判断
字符串
是否包含子
字符串
的
方法
。分享给大家供大家参考。具体如下:
python
的string对象没有
contains
方法
,不用使用string.
contains
的
方法
判断是否包含子
字符串
,但是
python
有更简单的
方法
来替换
contains
函数。
方法
1:使用 in
方法
实现
contains
的功能:
site = '//www.jb51.net/'
if "jb51" in site:
print('site
contains
jb51')
输出结果:site
contains
jb51
方法
2:使用find函数实现
contains
的功能
s = "T
Python
语言里有许多(而且是越来越多)的高级特性,是
Python
发烧友们非常喜欢的。在这些人的眼里,能够写出那些一般开发者看不懂的高级特性,就是高手,就是大神。
但你要知道,在团队合作里,炫技是大忌。
为什么这么说呢?我说下自己的看法:
越简洁的代码,越清晰的逻辑,就越不容易出错;
在团队合作中,你的代码不只有你在维护,降低别人的阅读/理解代码逻辑的成本是
一个
良好的品德
简单的代码,只会用到最基本的语法糖,复杂的高级特性,会有更多的依赖(如语言的版本)
该篇是「炫技系列」的第三篇内容,在这个系
(转载)http://outofmemory.cn/code-snippet/14513/
python
-decide-charaeter--string-if-contain-
contains
--charaeter-method
方法
1:使用 in
方法
实现
contains
的功能:
site = 'http://www.outofmemory.cn/'
if "sharejs" in
Python
String class has __
contains
__() function that we can use to check if it
contains
another string or not.
Python
字符串
类具有__
contains
__()函数,我们可以使用该函数检查它是否包含另
一个
字符串
。
Python
字符串
包含 (
Python
String contain...
2.
contains
来源
contains
方法
在 operator 模块中,operator模块是
Python
中内置的操作符接口,它定义了一些算数、比较、逻辑、序列运算的函数,operator 模块是用 C语言 实现的,所以执行速度比
Python
代码快。
3.
contains
用法举例
# 导入operator模块
import operator
#调用 operat...
String inputString = "hello there, william";
复制代码我们的任务是查找inputString 是否包含“hello”和“william”字样。
所以,让我们把我们的
关键字
放到
一个
数组中:
String[] words = {"hello", "william"...
python
判断
字符串
(string)是否包含(
contains
)子
字符串
的
方法
python
的string对象没有
contains
方法
,不用使用string.
contains
的
方法
判断是否包含子
字符串
,但是
python
有更简单的
方法
来替换
contains
函数。
方法
1:使用 in
方法
实现
contains
的功能:site = 'http://www.sharejs.com/'
if "sharejs"