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

Discussion in 'Python' started by Peter Otten, Sep 24, 2010.

  1. Peter Otten

    Peter Otten Guest

    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
     
    Peter Otten, Sep 24, 2010
    #1
    1. Advertising

  2. Peter Otten

    John Posner Guest

    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/read/functional-programming.html
     
    John Posner, Sep 24, 2010
    #2
    1. Advertising

  3. Peter Otten

    Ethan Furman Guest

    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[pos:pos+length] == list1:
    return True
    pos += 1

    ~Ethan~
     
    Ethan Furman, Sep 24, 2010
    #3
  4. Peter Otten

    Tim Chase Guest

    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[pos:pos+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
     
    Tim Chase, Sep 24, 2010
    #4
  5. Peter Otten

    John Posner Guest

    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
     
    John Posner, Sep 24, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Dyer
    Replies:
    5
    Views:
    648
  2. Replies:
    5
    Views:
    241
    Steven Bethard
    Jun 12, 2007
  3. Dennis Lee Bieber

    Re: Feature request: String-inferred names

    Dennis Lee Bieber, Nov 26, 2009, in forum: Python
    Replies:
    4
    Views:
    313
    Carl Banks
    Dec 1, 2009
  4. Chris Rebert
    Replies:
    4
    Views:
    294
    Cameron Simpson
    Nov 27, 2009
  5. Gabriel Genellina

    Re: Feature request: String-inferred names

    Gabriel Genellina, Nov 26, 2009, in forum: Python
    Replies:
    38
    Views:
    757
    Bruno Desthuilliers
    Dec 4, 2009
Loading...

Share This Page