[Tutor] Finding items in a list

Kent Johnson kent37 at tds.net
Fri Feb 24 22:59:52 CET 2006


Emily,

As Danny suggests, a concrete example of your input and output would be 
very helpful.

It sounds like you have some data that looks like
CLUSTER 1
SOMETHING ELSE
CLUSTER 5
MORE STUFF
ETC

and you would like to make a list of [1, 5] - the data after 'CLUSTER'. 
Is that right? Here is something to help you get started. You can easily 
iterate through the lines in your data and select the ones that start 
with 'CLUSTER':

  >>> for line in data:
  ...   if line.startswith('CLUSTER'):
  ...     print line
  ...
CLUSTER 1
CLUSTER 5

Do you know enough string manipulation to know how to split up the line 
and get the data you want? Once you have that you can add it to a list.

Kent

Emily Patek wrote:
> Hi - 
> I am trying to break apart a list into several smaller lists based on repeating nearly identical entries.  For example, every so often there is the word CLUSTER with a number after it, like CLUSTER 1.  I can find them one by one, but would like to
> do a while loop for while item in list = CLUSTER X.  Is there a key that I can put in that means "any text that comes after" that would go from cluster to cluster without my typing in each cluster and number?  I am a very beginner programmer and am
> trying to parse a file of gene-related information that is in a not-so-easy format.  I put it into a list based on lines through the splitlines() method.  I also thought about splitting the text by CLUSTER and then making each item of the list into
> a string that I could then split, but couldn't get that to work either...
> 
> Thanks! 
> Emily
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list