Home > OS >  How to Get Rid of Fields in a Dictionary of Dictionaries?
How to Get Rid of Fields in a Dictionary of Dictionaries?

Time:01-29

I'm making this function on Python called clean_data that has...

  • Parameters: A dictionary of dictionaries (all strings), and a list of strings containing the fields we care about.

  • Returns: A dictionary of dictionaries with only the fields we care about, and with appropriate data types.

Fields we care about:

  • Opponent (string)
  • Power Plays (“PP” -- int)

Sample input:

{"1/1" : {"opponent" : "BU", "X" : "3", "PP" : "0"},
 "1/2" : {"opponent" : "HC", "X" : "4", "PP" : "1"},
 "1/5" : {"opponent" : "BC", "X" : "8", "PP" : "0"}}
["opponent", "PP"]

Expected output:

{"1/1" : {"opponent" : "BU", "PP" : 0},
 "1/2" : {"opponent" : "HC", "PP" : 1},
 "1/5" : {"opponent" : "BC", "PP" : 0}}

Currently, I have

def clean_data(my_dict, field_ls):
  t = {}
  for x in field_ls:
      field = x
  for line in my_dict:
      for i in (my_dict[line]):
          if i != field:
             my_dict[line].pop(i)

However, I don't quite seem to get the output I want as the pop(i) isn't really working. How can I fix my problem?

CodePudding user response:

Instead of popping keys, it seems it's easier (since there's only two of them) to select the key-value pairs you actually want.

def clean_data(my_dict):
    out = {}
    for k,d in my_dict.items():
        out[k] = {'opponent': str(d['opponent']), 'PP': int(d['PP'])}
    return out

Output:

{'1/1': {'opponent': 'BU', 'PP': 0},
 '1/2': {'opponent': 'HC', 'PP': 1},
 '1/5': {'opponent': 'BC', 'PP': 0}}

If you absolutely have to pop keys from a dictionary, then you can copy the keys using list constructor, then iterate over that list and pop unwanted fields

def clean_data(input_dict, dict_of_fields):
    my_dict = input_dict.copy()
    for d in my_dict.values():
        for field in list(d.keys()):
            if field not in dict_of_fields:
                d.pop(field)
            else:
                d[field] = dict_of_fields[field](d[field])
    return my_dict

Example:

sample_data = {"1/1" : {"opponent" : "BU", "X" : "3", "PP" : "0"},
 "1/2" : {"opponent" : "HC", "X" : "4", "PP" : "1"},
 "1/5" : {"opponent" : "BC", "X" : "8", "PP" : "0"}}
fields = {"opponent": str, "PP": int}

Output:

>>> print(clean_data(sample_data, fields))
{'1/1': {'opponent': 'BU', 'PP': 0}, 
 '1/2': {'opponent': 'HC', 'PP': 1}, 
 '1/5': {'opponent': 'BC', 'PP': 0}}
  •  Tags:  
  • Related