puncrazy
puncrazy

Reputation: 349

Dynamically changing key value in dictionary

I am checking the key in dictionary, if it contains space remove it.

def query_combination(sentence,mydict):
    for key in mydict.keys():
        if key == 'key':
            pass
        else:
            print 'key is : ',key
            if " " in key:                
                temp = key
                key = key.replace(' ',"")
                print 'new key : ',key                    
                sentence = sentence.replace(temp ,key)
                print 'new sentence : ',sentence
    print mydict
mydict = {'films': {'match': ['Space', 'Movie', 'six', 'two', 'one']}, u'Popeye Doyle': {'score': 100, 'match': [u'People', 'heaven', 'released']}}
sentence ='What films featured the character Popeye Doyle'
combination = query_combination(sentence,mydict)

I could not dynamically change the new key value to mydict. Any suggestion much appreciable

Upvotes: 0

Views: 240

Answers (3)

mhawke
mhawke

Reputation: 87074

key = key.replace(' ',"") does not affect the actual key in the dictionary, it is changing a copy of that key. You need to add the value to the dictionary with the new key and remove the old key. Here's one way to do it:

def query_combination(sentence, mydict):
    for old_key, new_key in [(key, key.replace(' ', '')) for key in mydict if ' ' in key]:
        mydict[new_key] = mydict.pop(old_key)
        sentence = sentence.replace(old_key, new_key)

Note, however, that you are replacing the key in the string sentence, but sentence is local to function query_combination(), so the outer scope sentence is unaffected by the replacement. I am not sure if that was what you hoped your code would do, but if it was you could simply return the revised sentence from the function, or include it as an item in the dictionary.

Given that sentence is not actually updated by your function, you can simplify the whole function to a mere dictionary comprehension:

>>> mydict = {'films': {'match': ['Space', 'Movie', 'six', 'two', 'one']}, u'Popeye Doyle': {'score': 100, 'match': [u'People', 'heaven', 'released']}}
>>> mydict = {key.replace(' ', '') : value for key, value in mydict.items()}
>>> mydict
{'films': {'match': ['Space', 'Movie', 'six', 'two', 'one']}, u'PopeyeDoyle': {'score': 100, 'match': [u'People', 'heaven', 'released']}}

Upvotes: 0

wenzul
wenzul

Reputation: 4048

You could try this

def query_combination(sentence,mydict):
    for key in mydict.iterkeys():
        if " " in key:                
            temp = key
            mydict[key.replace(" ","")] = mydict[key] # create new key
            del mydict[key] # delete old key
            sentence = sentence.replace(temp ,key)

Another solution in one line would be

mydict[key.replace(" ","")] = mydict.pop(key)

Upvotes: 1

TessellatingHeckler
TessellatingHeckler

Reputation: 28983

If you get a string out of the dictionary, and then change it and make a new string, the dictionary won't know about it; you can add a new entry to the dictionary and remove the old one:

        if " " in key:                
            newkey = key.replace(' ',"")
            mydict[newkey] = mydict[key]
            del mydict[key]

            print 'new key : ', newkey

Upvotes: 1

Related Questions