Created
July 3, 2018 17:15
-
-
Save kidpixo/8d257abb73d006ba73a169648cc6aef7 to your computer and use it in GitHub Desktop.
extract_list_to_dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# goal : extract a list of string based on first char. | |
# use case : parsing argument given to a cli command. | |
from itertools import groupby | |
arg_tags = ['!dog', 'dog', '!Monty'] # input values | |
{{1:'exclude',0:'include'}[k]: # key value from dict | |
list(g) # values from groupby | |
for k,g in groupby( | |
sorted( | |
arg_tags, # argument to sort | |
key=lambda i : i[0] # based on first char | |
) , | |
lambda i : i[0] == '!' # groupby function function | |
) | |
} | |
# Out: {'exclude': ['!dog', '!Monty'], 'include': ['dog']} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment