search in a list??

Peter Hansen peter at engcorp.com
Tue Aug 20 01:25:51 CEST 2002


jubafre at brturbo.com wrote:
> 
> i have a list:
> x=['LDA', 'D1', 'ADD', 'D2', 'STA', 'D3', 'HLT', 'D1:', 'DB', '3', 'D2:', 'DB', '2', 'D3:', 'DB', '0']
> 
> i want to indentify de nodes with ":", how i can get this?????
> 
> for example:
> 
> the node 7 -> "D1:" have de two points, but i don´t know how to get this information.

Others have respond about identifying the nodes with ":" but that doesn't 
necessarily help you with your real question, about counting the number 
of "nodes" after each one with a ":".  This might help:

>>> x= ['lda', 'd1', 'add', 'd2', 'sta', 'd3', 'hlt', 'd1:', 
...   'db', '3', 'd2:', 'db', '2', 'd3:', 'db', '0']
>>> hist = {}
>>> node = None
>>> for item in x:
...   if item.find(':') < 0:
...     hist[node] = hist.setdefault(node, 0) + 1
...   else:
...     node = item
...
>>> hist
{None: 7, 'd2:': 2, 'd3:': 2, 'd1:': 2}
>>>

The "None" entry indicates there are seven nodes that were not preceded
by a node with a ":" in it...

-Peter



More information about the Python-list mailing list