[Tutor] Finding all locations of a sequence

Teresa Stanton tms43 at clearwire.net
Thu Jun 14 21:48:52 CEST 2007


OK, I'm going to take a shot at this.  If what I'm understanding is correct,
a dictionary might help.  But that would depend on the format of the
original sequence.  If you have a list:

Lst1 = ['cow', 'pig', 'chicken', 'poultry', 'beef', 'pork']

Then you could:

Lst1.index('chicken')

And get 2, because the list starts with 0,  not 1, as the first index.

Or this:

>>> for i in Lst1:
	if i == 'chicken':
		print Lst1.index(i)
	if i == 'poultry':
		print Lst1.index(i)

		
2
3

Now, Kent or Alan and perhaps others will have a much more sophisticated way
of doing this same problem.  I'm still not exactly sure what it is you are
looking for, because there isn't enough information for me to really get a
grasp on your problem. My response is a simple list structure that has
simple operations.

Hope it helps :)

T

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
Of Lauren
Sent: Thursday, June 14, 2007 11:35 AM
To: tutor at python.org
Subject: [Tutor] Finding all locations of a sequence

Ok, please bear with me, I'm very new to programming and python. And
my question is rather...convoluted.

I have a bunch of sequences (about 4100 or so), and want to know where
they are in a very, very large string of letters. But wait, there's
more. Some of these sequences match to more than 'word' (for
example...to be consistent with later, chicken could match to poultry
or chicken).

example of what I want to do (on a much smaller scale):

Say I have chicken and I want to know where it occurs in a string of
words, but I want it to match to both chicken and poultry and have the
output of:

chicken  (locations of chicken and poultry in the string)

or something like that.

The string I'm dealing with is really large, so whatever will get
through it the fastest is ideal for me.

I hope this all makes sense...

If it's possible to have pseudocode that would be helpful.
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor





More information about the Tutor mailing list