0

So I have a huge list of dictionaries and I want to:

  • change asterixs into the integer "4"
  • change all spaces "" to the integer "0"
  • change all other numbers to integers

(this is only a portion of the list)

 clean_dict= [{'origin': 'Various/Unknown', 'idps': '', 'year': '1951', 'total': '180000', 'stateless': '', 'ret_refugees': '', 'asylum': '', 'country': 'Australia', 'others': '', 'ret_idps': '', 'refugees': '180000'}, {'origin': 'Various/Unknown', 'idps': '', 'year': '1951', 'total': '282000', 'stateless': '', 'ret_refugees': '', 'asylum': '', 'country': 'Austria', 'others': '', 'ret_idps': '', 'refugees': '282000'}]

I tried this code but nothing happened, any help is very appreciated!

Also pandas library is not allowed for this assignment.

 for name, datalist in clean_data.iteritems():
    for datadict in datalist:
        for key, value in datadict.items():
            if value == "*":
                datadict[key] = int(4)
            elif value == "":
                datadict[key]= int(0)
            else:
                value == int(value)
3
  • 1
    Plesse post a snippet of dictionary you are trying to clean. It is not clear if you need to clean values or keys or both. Commented Oct 13, 2016 at 4:56
  • Do you want to change * to 4 if the key contains * or if the key is *? Your question would be clearer if you provided sample input which contained all the required characters to be changed (I don't see any asterisks or spaces in your input dict) and the required output! Also space would be ' ' not '', which is an empty string. Commented Oct 13, 2016 at 22:07
  • You guys are all right! I just passed the wrong constructor through the function. Commented Oct 17, 2016 at 23:21

2 Answers 2

1

Assuming you're using Python 3 try this. For Python 2.x the main difference is to use iteritems() instead of items() but the rest should remain the same.

for dict in clean_dict:
    for k,v in dict.items():
        if v == '*':
            dict[k] = 4
        elif v == '':
            dict[k]= 0
        else:
            # not necessarily an integer so handle exception
            try:
                dict[k] = int(v)
            except ValueError:
                pass
Sign up to request clarification or add additional context in comments.

Comments

0

I guess this is what you want.

clean_dict= [{'origin': 'Various/Unknown', 'idps': '', 'year': '1951', 'total': '180000', 'stateless': '', 'ret_refugees': '', 'asylum': '', 'country': 'Australia', 'others': '', 'ret_idps': '', 'refugees': '180000'}, {'origin': 'Various/Unknown', 'idps': '', 'year': '1951', 'total': '282000', 'stateless': '', 'ret_refugees': '', 'asylum': '', 'country': 'Austria', 'others': '', 'ret_idps': '', 'refugees': '282000'}]
for datadict in clean_dict:
    for key, value in datadict.items():
        if value == '*':
            datadict[key] = 4
        elif value == '':
            datadict[key] = 0
        else:
            try:
                datadict[key] = int(value)
            except:
                continue

Changes explained:

  • int(4) or int(0) is unneccessary.
  • value == int(value) does nothing, i.e, it doesn't update your list or dictionary.
  • for name, datalist in clean_data.iteritems(): name is unused and also this does not help to update your list.
  • try and except: is used because string value such as Australia cannot be converted to int type.

1 Comment

you may do it in one line under try - datadict[key] = int({'*': 4, "": 0}.get(value, value))

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.