Implement a group_by_owners dictionary

15,748

Solution 1

Holly molly, that's a lot of code for something so simple:

def group_by_owners(files):
    result = {}
    for file, owner in files.items():  # use files.iteritems() on Python 2.x
        result[owner] = result.get(owner, []) + [file]  # you can use setdefault(), too
    return result

files = {
    'Input.txt': 'Randy',
    'Code.py': 'Stan',
    'Output.txt': 'Randy'
}

print(group_by_owners(files))
# {'Stan': ['Code.py'], 'Randy': ['Output.txt', 'Input.txt']}

You can simplify it even further by using collections.defaultdict for result and initializing all its keys to list - then you don't even need to do the acrobatics of creating a new list if it's not already there before appending to it.

Solution 2

I personally found the upvoted answer hard to understand and some others a bit bulky. Here is my version:

def group_by_owners(files):

ownerdict = {}

for key, value in files.items():
    if value in ownerdict:
        ownerdict[value].append(key)
    else:
        ownerdict[value] = [key]
return ownerdict


files = {
    'Input.txt': 'Randy',
    'Code.py': 'Stan',
    'Output.txt': 'Randy'
}
print(group_by_owners(files))

Solution 3

This worked for me. I believe this would be more efficient and simpler one.

# Input dictionary.
files = {
        'Input.txt': 'Randy',
        'Code.py': 'Stan',
        'Output.txt': 'Randy'
    }   
# Function to group the files.   
def group_by_owners(files):

    # Dictionary object to hold the result.
    result = {}

    for key, value in files.items():

        if value not in result.keys():

            # Insert the values into the resulting dictionary
            # by interchanging the key, values.
            result[value] = [key]
        else:

            # Append the othet file name if the owner is same.
            result[value].append(key)

    return result

print(group_by_owners(files))

Here is the output:

{'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}
Share:
15,748
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin almost 2 years

    I made a successful attempt on the TestDome.com Fileowners problem and wanted to see if anyone had suggestions to simplify my answer. The online IDE uses Python 3.5.1. If you are trying to do the problem yourself and are just looking for an answer, here's one. I am by know means a Python expert so this took me quite a while to produce with lots of tinkering. Any comments at all would be helpful even if its about syntax or general cleanliness. THANKS!

    Implement a group_by_owners function that:

    Accepts a dictionary containing the file owner name for each file name. Returns a dictionary containing a list of file names for each owner name, in any order. For example, for dictionary {'Input.txt': 'Randy', 'Code.py': 'Stan', 'Output.txt': 'Randy'} the group_by_owners function should return {'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}.

    class FileOwners:
    
    @staticmethod
    def group_by_owners(files):
        val = (list(files.values()))                    #get values from dict
        val = set(val)                                  #make values a set to remove duplicates
        val = list(val)                                 #make set a list so we can work with it
        keyst = (list(files.keys()))                    #get keys from dict
        result = {}                                     #creat empty dict for output
        for i in range(len(val)):                       #loop over values(owners)
            for j in range(len(keyst)):                 #loop over keys(files)
                if val[i]==list(files.values())[j]:     #boolean to pick out files for current owner loop
                    dummylist = [keyst[j]]              #make string pulled from dict a list so we can add it to the output in the correct format
                    if val[i] in result:                #if the owner is already in the output add the new file to the existing dictionary entry
                        result[val[i]].append(keyst[j]) #add the new file
                    else:                               #if the owner is NOT already in the output make a new entry 
                        result[val[i]] = dummylist      #make a new entry
        return result
    
    files = {
    'Input.txt': 'Randy',
    'Code.py': 'Stan',
    'Output.txt': 'Randy'
    }
    
    print(FileOwners.group_by_owners(files))
    

    Output:

    {'Stan': ['Code.py'], 'Randy': ['Output.txt', 'Input.txt']} 
    
  • chevybow
    chevybow over 5 years
    An explanation to the code will make for better answer
  • Nic3500
    Nic3500 over 5 years
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
  • Johan
    Johan almost 5 years
    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.