This is my Ruby code. I want to convert this function to a Python 3 equivalent.
files = {
'Input.txt' => 'Randy',
'Code.py' => 'Stan',
'Output.txt' => 'Randy'
}
def group_by_owners(files)
files.each_with_object({}) { |(key, value), new_hash| (new_hash[value] ||= []) << key }
end
puts group_by_owners(files)
The Ruby result looks like this:
{"Randy" => ["Input.txt", "Output.txt"], "Stan" => ["Code.py"]}
Python would be:
{"Randy": ["Input.txt", "Output.txt"], "Stan": ["Code.py"]}
Here is what I have tried:
def group_by_owners(files):
new_dict = dict(zip(files.values(), (files.keys())))
print(new_dict)
Then I was trying to append the keys to an array.
def group_by_owners(files):
keys_array = []
new_dict = dict(zip(files.values(), keys_array.append((files.keys()))))
but I don't think that will work inside of a zip method.