Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: feature request: string.contains('...')

Reply
Thread Tools

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

 
 
Peter Otten
Guest
Posts: n/a
 
      09-24-2010
Wim Feijen wrote:

> I was wondering, how to make a feature request?


You can post suggestions to improve python on the python-ideas mailing list
or make feature requests on the bugtracker at bugs.python.org

> I would really like having a string.contains('...') function which returns
> either True or False. I know I can mimick this behaviour by saying
> string.find('...') != -1 , however, I find this harder to read.
>
> string.contains('...') is easier to understand and more closely resembles
> natural language.


In modern python those functions provided by the string module that are also
available as methods should not be used anymore. Instead of

string.find(s, substr)

write

s.find(substr)

If you are not interested in the position of the substr use the "in"
operator:

if substr in s:
print "found"
else:
print "not found"

Peter

 
Reply With Quote
 
 
 
 
John Posner
Guest
Posts: n/a
 
      09-24-2010
On 9/24/2010 4:21 AM, Peter Otten wrote:

>
> If you are not interested in the position of the substr use the "in"
> operator:
>
> if substr in s:
> print "found"
> else:
> print "not found"
>


Another "missing feature" candidate: sublist

>>> 'bc' in 'abcde'

True
>>> list('bc') in list('abcde')

False


A little Googling indicates that Haskell *does* have this feature: [1]


ghci> [2,6] `isInfixOf` [3,1,4,1,5,9,2,6,5,3,5,8,9,7,9]
True

I haven't located an equivalent Python idiom, though. I took a quick
look through numpy, but no luck. Can anyone help?

Tx,
John

[1] http://book.realworldhaskell.org/rea...ogramming.html
 
Reply With Quote
 
 
 
 
Ethan Furman
Guest
Posts: n/a
 
      09-24-2010
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?

def listinlist(list1, list2):
"checks if list1 is in list2"
if not list1:
return True
if not list2:
return False
length = len(list1)
pos = 0
while True:
try:
pos = list2.index(list1[0], pos)
except ValueError:
return False
if list2[posos+length] == list1:
return True
pos += 1

~Ethan~
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-24-2010
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?
>
> def listinlist(list1, list2):
> "checks if list1 is in list2"
> if not list1:
> return True
> if not list2:
> return False
> length = len(list1)
> pos = 0
> while True:
> try:
> pos = list2.index(list1[0], pos)
> except ValueError:
> return False
> if list2[posos+length] == list1:
> return True
> pos += 1


Which I suppose could be rewritten something like

def listinlist(l1, l2):
len1 = len(l1)
offsets_to_consider = 1 + len(l2) - len1
return any(
l1 == l2[i:i+len1]
for i in xrange(offsets_to_consider)
)

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

list_in_list = lambda l1, l2: any(l1 == l2[i:i+len(l1)] for i
in range(1 + len(l2) - len(l1)))

-tkc




 
Reply With Quote
 
John Posner
Guest
Posts: n/a
 
      09-24-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
API for signal strength value with zero configuration feature on? =?Utf-8?B?a2Fu?= Wireless Networking 3 09-04-2005 11:13 PM
Disable the wireless network feature =?Utf-8?B?UmFqYXNla2FyYW4=?= Wireless Networking 1 05-04-2005 01:46 AM
Begging for a new feature! Alan P Firefox 2 04-03-2004 09:34 AM
bug or feature? Pedro Graca Firefox 3 01-02-2004 06:42 PM
feature question Jacob Farr Firefox 2 11-13-2003 09:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57