searching structures

Marc Boeren M.Boeren at guidance.nl
Tue Oct 21 11:58:10 EDT 2003


> I have next structures:
>  
> A[1,2,3]
> AB[4,5,6]
> ABC[7,8,9]
> ABCD[1,2,3]
> ABCDE[4,5,6]
> ABCDEF[7,8,9]

> I'd like to know how many 'arrays' start with ABCD like ABCD*
> (ABCD,ABCDE,ABCDEF)
 
> Is it possible to do it using Python?

Do you mean something like this:

>>> A=[1,2,3]
>>> AB=[4,5,6]
>>> ABC=[7,8,9]
>>> ABCD=[1,2,3]
>>> ABCDE=[4,5,6]
>>> ABCDEF=[7,8,9]
>>> [name for name in locals() if name.startswith('ABCD')]
['ABCD', 'ABCDE', 'ABCDEF']

You can get the names of the arrays in the current scope using locals()
and/or globals(), so regexes instead of the simple .startswith() should be
easy too.

Cheerio, Marc.





More information about the Python-list mailing list