Re: Rita Sue and Bob too

Discussion in 'Python' started by Cyrille Lavigne, Aug 20, 2004.

  1. If you dont want them in order you should do:
    >if "Rita" and "Sue" and "Bob" in list:
    > print "They were found"
    >else:
    > print "They are not in the list"

    Otherwise:
    >c,a=0,0
    >while c<len(list):
    > if list[c]=="Rita":
    > if list[c+1]=="Sue":
    > if list[c+2]=="Bob":
    > print "They were found"
    > a=1
    > c=c+1
    >if a!=1:
    > print "they are not in the list"

    but I'm sure there is a better way of doing this.
    Cyrille Lavigne, Aug 20, 2004
    #1
    1. Advertising

  2. Cyrille Lavigne wrote:
    > If you dont want them in order you should do:
    >
    >>if "Rita" and "Sue" and "Bob" in list:
    >> print "They were found"
    >>else:
    >> print "They are not in the list"


    Wrong !
    see:

    In [6]: 'Rita' and 'Sue' and 'Bob' in ['Bob']
    Out[6]: True

    >
    > Otherwise:
    >
    >>c,a=0,0
    >>while c<len(list):
    >> if list[c]=="Rita":
    >> if list[c+1]=="Sue":
    >> if list[c+2]=="Bob":
    >> print "They were found"
    >> a=1
    >> c=c+1
    >>if a!=1:
    >> print "they are not in the list"

    >
    > but I'm sure there is a better way of doing this.
    >

    This second piece of code searches list for the sequence
    ['Rita','Sue','Bob'] in this order and without inserts.
    It can be rewritten in a more pythonic way :

    for i in range(len(l)):
    if l[i:i+3] == ['Rita','Sue','Bob']:
    print 'They were found'
    break
    else:
    print 'They were not in the list'

    --
    Grégoire Dooms
    =?ISO-8859-1?Q?Gr=E9goire_Dooms?=, Aug 20, 2004
    #2
    1. Advertising

  3. Grégoire Dooms wrote:
    > This second piece of code searches list for the sequence
    > ['Rita','Sue','Bob'] in this order and without inserts.
    > It can be rewritten in a more pythonic way :
    >
    > for i in range(len(l)):
    > if l[i:i+3] == ['Rita','Sue','Bob']:
    > print 'They were found'
    > break
    > else:
    > print 'They were not in the list'


    Décidément, le monde est petit !

    Nicolas Ernotte, FSA12 ;-)
    Nicolas Ernotte, Aug 20, 2004
    #3
  4. On 19 Aug 2004 19:10:34 -0700, Cyrille Lavigne <> wrote:
    > If you dont want them in order you should do:


    If you don't want them in order, then the sets module is probably the
    clearest way to go:

    from sets import Set

    names = ["Rita", "Jim", "Bob", "Jane", "Sue", "Jerry"]
    searchnames = ["Rita", "Bob", "Sue"]

    nameset = Set(names)
    searchnameset = Set(searchnames)
    if searchnameset.issubset(nameset):
    print "They're all found"
    else:
    print "They're not all found"

    -- Or you can eschew the temporary variables, and just say use
    Set(searchnames).issubset(Set(names)), but this is a little less
    clear.
    Andrew Durdin, Aug 21, 2004
    #4
    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. M. Clift

    Rita Sue and Bob too

    M. Clift, Aug 20, 2004, in forum: Python
    Replies:
    13
    Views:
    618
    Shane Holloway (IEEE)
    Aug 23, 2004
  2. Ben Last

    RE: Rita Sue and Bob too

    Ben Last, Aug 20, 2004, in forum: Python
    Replies:
    3
    Views:
    300
    Neal Holtz
    Aug 20, 2004
  3. Ben Last

    RE: Rita Sue and Bob too

    Ben Last, Aug 20, 2004, in forum: Python
    Replies:
    2
    Views:
    380
    M. Clift
    Aug 20, 2004
  4. http://clearblogs.com/rita/24759/

    http://clearblogs.com/rita/24759/

    http://clearblogs.com/rita/24759/, Jan 5, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    330
    http://clearblogs.com/rita/24759/
    Jan 5, 2007
  5. Tim Streater

    @RH - Messrs Sue, Grabbit, and Runne

    Tim Streater, Jun 21, 2010, in forum: C Programming
    Replies:
    0
    Views:
    280
    Tim Streater
    Jun 21, 2010
Loading...

Share This Page