5

I have a dictionary that looks like:

d = {'alleged': ['truths', 'fiels', 'fact', 'infidelity', 'incident'],
 'greased': ['axle', 'wheel', 'wheels', 'fields', 'enGine', 'pizza'],
 'plowed': ['fields', 'field', 'field', 'incident', '', '']}

I would like to go over it and replace some items for another strings. The strings to be find and the ones to replace them are also in a dictionary, in which the key is the string to be found and the value is the string to replace it:

d_find_and_replace = {'wheels':'wheel', 'Field': 'field', 'animals':'plants'}

I tried to use a function like:

def replace_all(dic1, dic2):
    for i, j in dic.items():
        dic3 = dic1.replace(i, j)
    return(dic3)

but it won't work because obviously, it uses replace built-in function replace inside it, and it is not possible to use it for dictionaries. Any suggestions on how to do this? Thank you very much for any help.

Edited to correct spelling mistakes.

2
  • 2
    your d_find_and_replace dict is a bit of a mess. Check that your quotes are in the right places. Commented Nov 13, 2013 at 18:26
  • 1
    your d dictionary also has several errors (no opening quote on alleged, close bracket inside the greased list). Clean those up so it works first then you can debug the replacement. Commented Nov 13, 2013 at 18:34

2 Answers 2

5

Try just using direct assignation:

for key in d:
    li = d[key]
    for i,item in enumerate(li):
        li[i] = d_find_and_replace.get(item, item)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. This goes over only line of the dictionaries. Maybe it need to incorporate list comprehension?
@user2962024 erm. I don't follow. This goes through every key in d and does a find_and_replace operation on every list item for that key. Did you want to build a new dict instead of mutating d in-place?
Thanks again for your time. I am sorry, I messed up something, but now I figured it out. Very nice solution! I really thank you for this!
-1

here is a solution. I also fixed your dictionaries they were messy. Check your spelling because with those given keys I think there is only going to be one match that will be replaced. for example engine will never match enGine, unless you don't care to match lower or upper case, in that case you can use if val.lowercase()

d = {'alleged': ['truths', 'fiels', 'fact', 'infidelity', 'incident'],
     'greased': ['axle', 'wheel', 'wheels', 'fields', 'enGine', 'pizza'],
     'plowed': ['fields', 'field', 'field', 'incident', '', '']}

d_find_and_replace = {'fields': 'field', 'engine': 'engine', 'incidint':'incident'}

keys_replace = d_find_and_replace.keys()
print d
for key in d.keys():
    for i, val in enumerate(d[key], 0):
        if val in keys_replace:
            index_to_replace = keys_replace.index(val)
            d[key][i] = d_find_and_replace[keys_replace[index_to_replace]]

print d

5 Comments

Thanks for your solution, but I think this is python 2, is it not? I get the error: AttributeError: 'dict_keys' object has no attribute 'index', do you know what is the equivalent for python 3.2? Thanks again!
I didnt use any dict_keys in the code I posted. Can you edit your answer with the code that is giving you the error.
I updated my answer, I think the problem was that I was using index as a local variable in the for loop and index as function. I replaced the index variable with i. Let me know if it works.
I am sorry, no difference, still getting the message: AttributeError: 'dict_keys' object has no attribute 'index' referring to index_to_replace = keys_replace.index(val)`.
weird, what does print keys_replace it should be a list ['engine', 'fields', 'incidint']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.