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']}
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With