Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Rita Sue and Bob too

Reply
Thread Tools

Rita Sue and Bob too

 
 
M. Clift
Guest
Posts: n/a
 
      08-20-2004
Hi All,

Can someone help. I promise I've looked how to do this but can't find a
way...

Ok, to find one name is easy

if 'Bob' in list:
print "They were found"
else:
print "They are not in list"

But, how to I find a sequence in a list of unknown size? i.e. this sequence
in list of other names and replace it with three others?

'Rita','Sue','Bob'

This is almost a nightly occurrence (my posting questions), but I am
learning : )


 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      08-20-2004
M. Clift wrote:
> Hi All,
>
> Can someone help. I promise I've looked how to do this but can't find a
> way...
>
> Ok, to find one name is easy
>
> if 'Bob' in list:
> print "They were found"
> else:
> print "They are not in list"
>
> But, how to I find a sequence in a list of unknown size? i.e. this sequence
> in list of other names and replace it with three others?
>
> 'Rita','Sue','Bob'
>
> This is almost a nightly occurrence (my posting questions), but I am
> learning : )


My first thought would be to scan the list for the first element
'Rita', and when you find it, take out a slice that is the
same size as the sequence you have there (use len() of course)
and compare the two. If they don't match, move on. The scanning
and "moving on" part would certainly best be done using .index()
and note that that function can take a second parameter which
specifies the starting index, so you can do a progressive search
efficiently. The actual code is left as an exercise to the
reader.

-Peter
 
Reply With Quote
 
 
 
 
Jeremy Jones
Guest
Posts: n/a
 
      08-20-2004
M. Clift wrote:

>Hi All,
>
>Can someone help. I promise I've looked how to do this but can't find a
>way...
>
>Ok, to find one name is easy
>
>if 'Bob' in list:
> print "They were found"
>else:
> print "They are not in list"
>
>But, how to I find a sequence in a list of unknown size? i.e. this sequence
>in list of other names and replace it with three others?
>
>'Rita','Sue','Bob'
>
>This is almost a nightly occurrence (my posting questions), but I am
>learning : )
>
>
>
>

I'm sure someone else can come up with something more elegant, but
here's a way you could do it:

>>> names

['larry', 'curly', 'moe', 'shimp', 'rita', 'sue', 'bob', 'billy', 'scott']
>>> for idx in range(len(names)):

.... if names[idx:idx + 3] == ['sue', 'bob', 'billy']:
.... print "found 'em at element", idx
.... break
....
found 'em at element 5
>>>


Notice, this:
>>> ['sue', 'bob', 'billy'] in names

False
>>>

returns false. The reason is, I think, because the set ['sue', 'bob',
'billy'] is not really a subset of ['larry', 'curly', 'moe', 'shimp',
'rita', 'sue', 'bob', 'billy', 'scott'], even though the three names
appear sequentially in both lists. But, this:

>>> names2

[['sue', 'bob', 'billy'], ['larry', 'curly', 'moe', 'shimp', 'rita',
'sue', 'bob', 'billy', 'scott']]
>>> ['sue', 'bob', 'billy'] in names2

True
>>>

does "work" for the same reason that it doesn't work in the first
example. The list ['sue', 'bob', 'billy'] itself is part of the larger
list, names2.

HTH,

Jeremy Jones

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      08-20-2004
"M. Clift" <(E-Mail Removed)> writes:
> But, how to I find a sequence in a list of unknown size? i.e. this sequence
> in list of other names and replace it with three others?
>
> 'Rita','Sue','Bob'
>
> This is almost a nightly occurrence (my posting questions), but I am
> learning : )


You have to scan through the list and look for that sequence, e.g.

for i in xrange(len(mylist) - 2):
if mylist[i:i+3] == ['Rita','Sue','Bob']:
print 'They were found'
break

There are fancier and more efficient ways to scan the list, but this
is the basic idea.
 
Reply With Quote
 
M. Clift
Guest
Posts: n/a
 
      08-20-2004
Thankyou to all of you, and so quick.

More for me to study : )

All the best,

M


 
Reply With Quote
 
Eugene Oden
Guest
Posts: n/a
 
      08-20-2004
M. Clift wrote:

> Hi All,
>
> Can someone help. I promise I've looked how to do this but can't find a
> way...
>
> Ok, to find one name is easy
>
> if 'Bob' in list:
> print "They were found"
> else:
> print "They are not in list"
>
> But, how to I find a sequence in a list of unknown size? i.e. this
> sequence in list of other names and replace it with three others?
>
> 'Rita','Sue','Bob'
>
> This is almost a nightly occurrence (my posting questions), but I am
> learning : )


here's my stab at it:

def findSequence(seq, search):
"""returns the index where search can be found in seq where search and
seq are both sequences.
"""
searchLength = len(search)

for i in range(len(seq)-searchLength+1):
compare = seq[i:i+searchLength+1]

if search == seq[i:i+searchLength]:
return i

raise ValueError, 'sequence %s is not in list' % str(search)

someList = ['Joe', 'Rita', 'Sue', 'Bob', 'Peter']
searchList = ['Rita', 'Sue', 'Bob']

i = findSequence(someList, searchList)
someList[i:i+len(searchList)] = ['Paul', 'John', 'James']
# someList is now ['Joe', 'Paul', 'John', 'James', 'Peter']

findSequence(someList, searchList) # raises ValueError

 
Reply With Quote
 
Jeffrey Froman
Guest
Posts: n/a
 
      08-20-2004
M. Clift wrote:

> But, how to I find a sequence in a list of unknown size? i.e. this
> sequence in list of other names and replace it with three others?


How about a generator?

def slice(apple, worm, new_worm):
while apple:
if worm == apple[:len(worm)]:
apple = apple[len(worm):]
yield new_worm
else:
yield [apple.pop(0)]

Jeffrey
 
Reply With Quote
 
Jeffrey Froman
Guest
Posts: n/a
 
      08-20-2004
Jeffrey Froman wrote:
> How about a generator?


Sorry, bad paste on my previous reply. The generator example should look
like:

def replace_in_list(apple, worm, new_worm):
while apple:
if worm == apple[:len(worm)]:
apple = apple[len(worm):]
for w in new_worm:
yield w
else:
yield apple.pop(0)



 
Reply With Quote
 
Michael J. Fromberger
Guest
Posts: n/a
 
      08-20-2004
In article <cg3ksb$bg5$(E-Mail Removed)>,
"M. Clift" <(E-Mail Removed)> wrote:

> Hi All,
>
> Can someone help. I promise I've looked how to do this but can't find a
> way...
>
> Ok, to find one name is easy
>
> if 'Bob' in list:
> print "They were found"
> else:
> print "They are not in list"
>
> But, how to I find a sequence in a list of unknown size? i.e. this sequence
> in list of other names and replace it with three others?
>
> 'Rita','Sue','Bob'
>
> This is almost a nightly occurrence (my posting questions), but I am
> learning : )


You've gotten several other answers, but I'd like to propose yet another
one:

def replace_sublist(L, S, T):
"""Replace each sublist of L equal to S with T. Also returns the
resulting list."""

assert(len(S) == len(T))

for p in [ x for x in xrange(len(L))
if L[x] == S[0] and L[x : x + len(S)] == S ]:
L[p : p + len(S)] = T

return L

In short, the list comprehension gives all the offsets in L where a copy
of S can be found as a sublist. Now, if it happens that some of these
positions overlap (e.g., L = [ 1, 1, 1 ] and S = [ 1, 1 ]), then the
results will be strange. But as long as your matches do not overlap,
this should work well.

-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
 
Reply With Quote
 
M. Clift
Guest
Posts: n/a
 
      08-20-2004
If I wasn't happy enough already with the examples/ideas you've all shown
me, how about searching as previously, but where the middle name could be
anything. i.e. 'Rita', 'anyname','Bob'

M


 
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
OT Rita - Digital Soft Paw on Flickr Re: THE CURE FOR RITA! Mark Thomas Digital Photography 24 07-25-2008 06:55 AM
To Rita A Berkowitz and group Gwyne­ Bennetdottir Digital Photography 1 12-31-2007 02:13 PM
Re: Rita Sue and Bob too Cyrille Lavigne Python 3 08-21-2004 12:13 AM
RE: Rita Sue and Bob too Ben Last Python 3 08-20-2004 04:50 PM
RE: Rita Sue and Bob too Ben Last Python 2 08-20-2004 12:06 PM



Advertisments