feature request: string.contains('...')

John Posner jjposner at optimum.net
Fri Sep 24 17:31:16 EDT 2010


On 9/24/2010 2:45 PM, Tim Chase wrote:
> On 09/24/10 13:01, Ethan Furman wrote:
>> John Posner wrote:
>>> Another "missing feature" candidate: sublist
>>>
>>> >>> 'bc' in 'abcde'
>>> True
>>> >>> list('bc') in list('abcde')
>>> False
>>
>> I'm not aware of any idioms, but how about a simple function?

<snip>

>
> Foldable into a one-line version if one's sick enough to use it:

<snip>


Looking at this a bit more, I can see why the *in* operator applies to 
strings, but not to lists. Consider the ambiguity in this 
"heterogeneous" list:

   mylist = [0, 1, 2, 3, [a, b], 10, 11, 12, a, b, 13]

Should the expression *[a, b] in mylist* get a hit at offset 4, or at 
slice [8:10]?

If you know that your lists will be homogeneous ("scalar" values only), 
or if you're willing to program around the potential ambiguity, then 
Ethan's function can easily be adapted into a __contains__() method of a 
*list* subclass.

Tx,
John



More information about the Python-list mailing list