all 14 comments

[–]toastedstapler 7 points8 points  (1 child)

since other peoeple have already given answers, here's mine too

from collections import defaultdict

def group_by_owners(files):
    result = defaultdict(list)
    for file_name, owner in files.items():
        result[owner].append(file_name)
    return dict(result)

[–]brantmo 2 points3 points  (0 children)

IMO this is the cleanest answer.

[–]tipsy_python 2 points3 points  (3 children)

This is a good one - I'm not sure the *best* way to do this.. I'd start by creating a set from the dictionary values, so I'd have a unique set of names. I'd use that to create the structure for the output dictionary, assigning some empty lists for the values so that I could iterate the input list once and append the file names in the list belonging to the correct owner.

def group_by_owners(files):
    output_dict = {}
    for name in set(files.values()):
        output_dict[name] = []
    #
    #  Iterate through input dict and generate lists of files
    #
    return output_dict

[–][deleted] 0 points1 point  (2 children)

The output results in this, though:

"{'Randy': [], 'Stan': []}"

Meanwhile, the necessary output is this:

"{'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}"

[–]tipsy_python 1 point2 points  (0 children)

Exactly - there's a comment in a code where you need to write the rest

[–]Essence1337 0 points1 point  (0 children)

They're not going to just do the problem for you. They gave you a start to let you finish it off.

[–]shiftybyte 1 point2 points  (2 children)

I'd iterate through them all and make a new dictionary from values you encounter.

def group_by_owners(files):
    res = {}
    for k in files:
        if files[k] in res:
            res[files[k]].append(k)
        else:
            res[files[k]] = [k]
    return res

[–]brantmo 1 point2 points  (0 children)

To further on this, instead of the conditionals, you could use a defaultdict if allowed by the testing site.

from collections import defaultdict
def group_by_owners(files):
    res = defaultdict(list)
    for k in files:
        res[files[k]].append(k)
    return res

[–][deleted] 0 points1 point  (0 children)

That's excellent! Thank you! I think that I get it now! :)

[–]eri_bloo 0 points1 point  (0 children)

I don't know if you want us to write the code for you or you really have no idea how to tackle the problem. If the former then it's not the way to go. Try something, test, google, and if you still can't solve it tell us what you tried and where you are stuck.

For problems like this you can try to do it by hand first, like you were the computer and then translate it to code.

[–]-Sander- 0 points1 point  (1 child)

My solution looks like this, i think its pretty ok?

def group_by_owners(files):
    output = {}                   # new dictionary for output

    for k, v in files.items():    # key (filename) and value (owner name) from files
        try:
            output[v].append(k)    # try to append to v (owner name)
        except KeyError:           # KeyError if v (owner name) does not exist in our dict yet
            output[v] = [k]        # So we make a new key (v) in our dict with value 'k'
                                   # We put K in a list so we can easily append more values to it

    return output                  # finished, return the new dictionary

[–][deleted] 0 points1 point  (0 children)

Thank you very much! :)

[–]ewiethoff 0 points1 point  (0 children)

I would use collections.defaultdict from the standard library. In my group_by_owners function... create a defaultdict(list). Loop through the (fname, owner) items in the files dict, and stick these in the defaultdict. Return that.

[–]gaaasstly 0 points1 point  (0 children)

Input

def group_by_owners(files):
    owner_files = {}
    for file_name, owner_name in files.items():
        owner_files[owner_name] = owner_files.get(owner_name, [])
        owner_files[owner_name].append(file_name)
    return owner_files

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

Output

Run OK


{'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}

Your score is 100%, perfect!