# Re: Rita Sue and Bob too

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

1. ### Cyrille LavigneGuest

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

2. ### =?ISO-8859-1?Q?Gr=E9goire_Dooms?=Guest

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

3. ### Nicolas ErnotteGuest

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
4. ### Andrew DurdinGuest

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