"str.contains(part)" or alternatives?

Bengt Richter bokr at oz.net
Mon Sep 16 18:10:26 EDT 2002


On Wed, 11 Sep 2002 21:18:23 -0700, Greg Fortune <lists at gregfortune.com> wrote:

><snip>> 
>>> I think the current proposal is to move to
>>> 
>>>     if ll in s:
>>> 
>>> for this construct. Happy?
>> 
>> I would like .contains more, but at least the proposal is _much_ better
>> than "if s.find(part) != -1". :-)
>> 
>> "if part in s" disturbs the pattern "if item in sequence" somewhat but
>> that has already been weakened by "if key in dict" (that I like,
>> nonetheless).
>> 
>> Stefan
>
>
>Why not simply use .count(part) > 0 ?  I didn't catch the first part of the 
>conversation, but if I'm looking for a contains, I just do.
>
>if(search_me.count(substr) > 0):
>        print 'found'
>else:
>        print 'not found'
>
>
Mostly it won't matter, but in cases like

    search_me = 'a'*10000000
    substr = 'a'

it will 'way suboptimal in comparison with find or index.

Regards,
Bengt Richter



More information about the Python-list mailing list