Skip to content

Instantly share code, notes, and snippets.

@PeterWaIIace
Created June 11, 2020 13:08
Show Gist options
  • Save PeterWaIIace/bbaf229de44ce07e5f30f19de1a2897a to your computer and use it in GitHub Desktop.
Save PeterWaIIace/bbaf229de44ce07e5f30f19de1a2897a to your computer and use it in GitHub Desktop.
Fileowners to filegroups - simple python excercise for list comprehension (I dont know the source)
# Excercise sent by friend - I dont know the source
# fo is dict with fileowners - objective is to transfer it to fg (file group) where user (ie Randy) is unique key
# and hold list of files
# fo = {"input.txt":"Randy","code.py":"Stan","Output.txt":"Randy"} -> fg = {"Randy":["input.txt","Output.txt"],"Stan":["code.py"]}
fo = {"input.txt":"Randy","code.py":"Stan","Output.txt":"Randy"}
fg = dict()
kelse = lambda fo,fg,k : fg.update({fo[k]:[k]})
kif = lambda fo,fg,k : fg[fo[k]].append(k)
lc_file_group = [kif(fo,fg,k) if fo[k] in fg else kelse(fo,fg,k) for k in fo.keys()]
print(fg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment