def parse_input(inp, sort_output=True, ensure_unique=True):
"""
Parse the user input to get a list of integers.
Parameters:
===========
- inp: string
Can be in the form 'a-b', 'a,b,c', 'a-b,c-d', etc.
'-' means an inclusive list of every number between a and b
',' means the numbers a and b
- sort_output: boolean
Sort the output integers?
- ensure_unique: boolean
Make sure the final list has no repeats?
:return: A list of integers
"""
sublists = inp.split(',')
final_list = []
for l in sublists:
if '-' in l:
first, last = l.split('-')
for i in range(int(first), int(last) + 1):
final_list.append(i)
else:
final_list.append(int(l))
if ensure_unique:
final_list = pd.unique(final_list)
if sort_output:
final_list = sorted(final_list)
return final_list
评论列表
文章目录