user1182090
user1182090

Reputation: 301

How to identify dict keys with spaces without removing whitespaces

suppose d is a dict

d={' a ':1,'b ':2}

if we give like this

d.has_key(' a ')
True

but this is false

d.has_key('a')
False

so i tried like this

d.has_key('\sa\s')
False

so how to find dict keys having spaces without stripping their whitespaces

thanks in advance

Upvotes: 0

Views: 5086

Answers (7)

martineau
martineau

Reputation: 123473

I think the concept shown in @astynax's answer -- deriving a dictionary subclass -- was on the right track however it's incomplete...probably because providing a complete dictionary subclass can be a lot of work since there are quite a few interrelated methods that would need to be overridden.

A shortcut to avoid that is to derive a subclass from the UserDict.DictMixin class instead of dict. The advantage is that it would reduce the number of methods that have to be written to a minimum of four. More efficiency can be obtained by also implementing several additional ones as an optimization.

Here's a complete working example that does this, resulting in something which behave almost exactly like a dictionary object except for the desired ignoring of any white-space around the keys. Except possibly when deleting keys, performance should be nearly as good as that of a standard dictionary, although it does use more memory.

import UserDict

class WhiteOut(UserDict.DictMixin):
    """ Mapping object with white space insensitive keys """
    def __init__(self, *args, **kwargs):
        self._dict = dict(*args, **kwargs)
        self._skeys = dict((k.strip(), v) for k, v in self._dict.iteritems())

    def __getitem__(self, key):
        try:
            return self._dict.__getitem__(key)
        except KeyError:
            return self._skeys.__getitem__(key.strip())

    def __setitem__(self, key, val):
        self._dict.__setitem__(key, val)
        self._skeys.__setitem__(key.strip(), val)

    def __delitem__(self, key):
        try:
            self._dict.__delitem__(key)
        except KeyError:
            stripped_key = key.strip()
            try:
                self._skeys.__delitem__(stripped_key)
            except KeyError:
                raise
            else:  # delete item corresponding to stripped_key in _dict
                for akey in self._dict:
                    if akey.strip() == stripped_key:
                        self._dict.__delitem__(akey)
                        return

    def keys(self):
        return self._dict.keys()

    def __iter__(self):
        return iter(self._dict)

    def __contains__(self, key):
        return self._skeys.__contains__(key.strip())

if __name__ == '__main__':
    wo = WhiteOut({'a':1, ' b':2})
    wo['  c  '] = 3
    print 'c' in wo # True
    print wo['c'] # 3
    print 'has_key:'
    print wo.has_key('  c  ')
    print wo.has_key('c')
    print wo['c '] # 3
    print 'wo.keys():', wo.keys()
    wokeys = [k for k in wo]
    print 'wo keys via iteration:', wokeys
    wo.pop("b")
    print 'wo.keys() after pop("b"):', wo.keys()
    try:
        wo.pop('not there')
    except KeyError:
        pass
    else:
        print "wo.pop('not there') didn't raise an exception as it should have"

Upvotes: 0

cblab
cblab

Reputation: 1935

You can use this generator expression: 'a' in (item.strip() for item in d.keys())

>>> d={' a ':1, 'b ':2}
>>> 'a' in (item.strip() for item in d.keys())
True
>>> 'b' in (item.strip() for item in d.keys())
True
>>> 'c' in (item.strip() for item in d.keys())
False
>>> d
>>> {' a ': 1, 'b ': 2}

edit

For accessing the value, you can:

>>> for key, value in d.iteritems():
if key.strip()=='a':
    print value
1

Or, one-liner version:

>>> [value for key, value in d.iteritems() if key.strip() == 'a'][0]
1
>>> [value for key, value in d.iteritems() if key.strip() == 'b'][0]
2

Basically, [value for key, value in d.iteritems() if key.strip() == 'b'] will return a list of values and [0] for selecting the first one. If you have several similar keys, like:

>>> d = {'a':1, ' a':2, ' a ':3}

Then you can do:

>>> values = [value for key, value in d.iteritems() if key.strip() == 'a']
>>> len(values)
3
>>> values[0]
1
>>> values[1]
2
>>> values[2]
3

Upvotes: 1

eumiro
eumiro

Reputation: 212915

You will have to remove them:

d = {' a ':1,'b ':2}
key = 'a'
print any(key == k.strip() for k in d.iterkeys())

prints True

To get the value, you can use this method:

def get_stripped(d, key):
    return next((v for k,v in d.iteritems() if key == k.strip()), None)

print get_stripped(d, 'a') # prints 1
print get_stripped(d, 'c') # prints None

This will return just one arbitrary value if d = {' a ': 1, ' a': 2} and key = 'a'.

Upvotes: 3

shiva
shiva

Reputation: 2770

d = {' a':'b',' c ':'c','b ':'b'}
key = ' a'
[d[l] for l in  d.iterkeys() if l.strip() == key.strip()]
['b']

Upvotes: 0

shihongzhi
shihongzhi

Reputation: 1931

>>>d = {' a ':1, 'b ':2}
>>>dd = dict((l.strip(), v) for l, v in d.iteritems())
>>>dd
{'a':1, 'b':2}
>>>'a' in dd
True
>>>d['a']
1

Upvotes: 0

Aleksei astynax Pirogov
Aleksei astynax Pirogov

Reputation: 2491

If you need it quite often, you can subclass dict:

class D(dict):
    def __init__(self, *args, **kwargs):
        super(D, self).__init__(*args, **kwargs)
        self._skeys = dict((k.strip(), v) for k, v in self.iteritems())

    def __setitem__(self, key, val):
        super(D, self).__setitem__(key, val)
        self._skeys[key.strip()] = key

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            return self[self._skeys[key]]

    def __contains__(self, key):
        return (dict.__contains__(self, key) or key in self._skeys)

usage:

d = D({'a':1, 'b':2})
d['  c  '] = 3
print 'c' in d # True
print d['c'] # 3

This class works fine, if key_n.strip() != key_m.strip() is True for any n != m

Upvotes: 1

Charles Beattie
Charles Beattie

Reputation: 5949

I would recommend you create a dictionary of stripped values if you need to test many.

>>> d={' a ':1,'b ':2}
>>> t = dict( (pair[0].strip(), pair) for pair in d.iteritems())
>>> print t.has_key('a')
True
>>> print t['a']
(' a ', 1)

Upvotes: 0

Related Questions