[SciPy-User] 2D nparray lookup with wild cards - sane usage of 'eval' ?

Robert Kern robert.kern at gmail.com
Sun Jun 7 16:57:00 EDT 2015


On Sun, Jun 7, 2015 at 9:32 PM, Bjorn Madsen <
bjorn.madsen at operationsresearchgroup.com> wrote:
>
> Hi Scipy users,
> I am looking for suggestions on the lookup function below. It uses 'eval'
which has a reputation of "being evil" - however I can't find a better
general way of implementing a lookup with wildcards than this. Any
suggestions?
>
> def lookup2d(keys, array, wildcard='?', return_value=True):
>     if len(keys)!=len(array[0]):
>         raise valueError("Wrong dimensionality: Key must have same length
as array[0]")
>
>     # query constructor
>     c = 'np.where( '
>     for idx,key in enumerate(keys):
>         if key is not wildcard:
>             c+= '(array[:,{idx}] == {key}) &'.format(idx=idx,key=key)
>     c = c.rstrip('&')
>     c += ' )'
>
>     # query execution
>     mask = eval(c)
>
>     if return_value == True:
>         return array[mask]
>     elif return_value == False:
>         return mask
>
> a = np.arange(1,13).reshape(3,4)
> # a = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
> a = np.concatenate( (a,a,a,a), axis=0)
>
> keys = [5,'?','?',8]
>
> print("a:\n", a)
> print("keys:", keys)
> print("Looking up keys in a:")
> print(lookup2d(keys, a, wildcard='?', return_value=False))
> print(lookup2d(keys, a, wildcard='?', return_value=True))

Recall that True is the identity value for the & operation.

def lookup2d(keys, array, wildcard='?', return_value=True):
    n = len(array[0])
    assert len(keys) == n  # I'm being lazy
    mask = np.ones(array.shape[:-1], dtype=bool)
    for idx, key in enumerate(keys):
        if key != wildcard:  # don't use "is" for strings and integers
            mask &= (array[:, idx] == key)
    mask = np.where(mask)
    if return_value:
        return array[mask]
    else:
        return mask

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150607/535e1c42/attachment.html>


More information about the SciPy-User mailing list