Given a stream of dictionaries each with unique numerical ID, what would be the most efficient way to generate a list (or a format map
-able into a sorted list) of IDs based on thresholds that do not increase linearly (<48, <103, <123...)? I've not looked extensively into itertools
or other useful iteration libraries and I can't think of a much better way to group other than using elif
s.
Example using if/elif/else:
dicts = [{'id':30},{'id':60},{'id':90},{'id':120},{'id':150}]
groups = [[] for _ in range(5)]
for a_dict in dicts:
ID = a_dict['id']
if ID < 50: groups[0].append(ID)
elif ID < 100: groups[1].append(ID)
elif ID < 150: groups[2].append(ID)
elif ID < 200: groups[3].append(ID)
else: groups[4].append(ID)
Output:
>>> print(groups)
[[30], [60, 90], [120], [150], []]