searching structures

Dave Kuhlman dkuhlman at rexx.com
Tue Oct 21 12:06:24 EDT 2003


Alberto Vera wrote:

> Hello:
> 
> 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?

Sounds like you want to filter on the *name* of the the array.  If
so, think about doing something like the following:

    >>> aa = 1
    >>> aab = 2
    >>> bb = 3
    >>>
    >>> dir()
    ['__builtins__', '__doc__', '__file__', '__name__', 'aa', 'aab',
'bb']
    >>> for x in dir():
    ...   if x[:2] == 'aa':
    ...     print 'yes', x
    ...   else:
    ...     print 'no', x
    ...
    no __builtins__
    no __doc__
    no __file__
    no __name__
    yes aa
    yes aab
    no bb

And if you need to distinguish the *type* of the value of the
variable, consider using type().

The built-in functions globals() and locals() give dictionaries
that contain variable name-value pairs.

These built-in functions are described at:

    http://www.python.org/doc/current/lib/built-in-funcs.html

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com




More information about the Python-list mailing list