Nitrodbz
Nitrodbz

Reputation: 1286

Python Group by count

Given a dictionary, I need some way to do the following:

In the dictionary, we have names, gender, occupation, and salary. I need to figure out if each name I search in the dictionay, there are no more than 5 other employees that have the same name, gender and occupation. If so, I output it. Otherwise, I remove it.

Any help or resources would be appreciated!

What I researched:

count = Counter(tok['Name'] for tok in input_file)

This counts the number of occurances for name (ie Bob: 2, Amy: 4). However, I need to add the gender and occupation to this as well (ie Bob, M, Salesperson: 2, Amy, F, Manager: 1).

Upvotes: 0

Views: 1025

Answers (1)

tmj
tmj

Reputation: 1868

To only check if the dictionary has 5 or more (key,value) pairs, in which the name,gender and occupation of employee is same, is quite simple. To remove all such inconsistencies is tricky.

# data = {}
# key = 'UID'
# value = ('Name','Male','Accountant','20000')
# data[key] = value

def consistency(dictionary):

    temp_list_of_values_we_care_about = [(x[0],x[1],x[2]) for x in dictionary.itervalues()]
    temp_dict = {}

    for val in temp_list_of_values_we_care_about:
        if val in temp_dict:
            temp_dict[val] += 1
        else:
            temp_dict[val] = 1

    if max(temp_dict.values()) >=5:
        return False
    else:
        return True

And to actually, get a dictionary with those particular values removed, there are two ways.

  1. Edit and update the original dictionary. (Doing it in-place)
  2. Create a new dictionary and add only those values which satisfy our constraint.
def consistency(dictionary):

    temp_list_of_values_we_care_about = [(x[0],x[1],x[2]) for x in dictionary.itervalues()]
    temp_dict = {}

    for val in temp_list_of_values_we_care_about:
        if val in temp_dict:
            temp_dict[val] += 1
        else:
            temp_dict[val] = 1

    new_dictionary = {}
    for key in dictionary:

        value = dictionary[key]
        temp = (value[0],value[1],value[2])

        if temp_dict[temp] <=5:
            new_dictionary[key] = value

    return new_dictionary

P.S. I have chosen the much easier second way to do it. Choosing the first method will cause a lot of computation overhead, and we certainly would want to avoid that.

Upvotes: 1

Related Questions