0

Suppose i have to following list, it is guaranteed to be sorted and have no duplicates:

In [224]: a=[0,2,4,8,12,16,20,21,30,33,36]

I want to subdivide it to form a nested tuple in which each item can be created using range or np.arange. There can sometimes be numerous grouping possibilities (two items can always form a group). I want to minimize the amount of groups containing two items.

Here is an example:

In [225]: to_range_group(a)
((0,2,4),(8,12,16,20),(21),(30,33,36))

What is the most pythonic and efficient way to accomplish this?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
snowleopard
  • 717
  • 8
  • 19
  • *I want to minimize the amount of groups containing two items* - and how about `((0,2,4),(8,12,16,20),(21,30,33,36))` ? Should it be an ascending subdivision? – RomanPerekhrest Nov 20 '16 at 17:50
  • `(21,30,33,36)` can't be created using `range` or `np.arange`. It does not consistently have an increment of 3. – snowleopard Nov 20 '16 at 17:51
  • With a little modification, [Identify groups of varying continuous numbers in a list](http://stackoverflow.com/questions/39709606/identify-groups-of-varying-continuous-numbers-in-a-list/39712094) has your answer – Moses Koledoye Nov 20 '16 at 17:53
  • Oh nice, thanks, couldn't find that. – snowleopard Nov 20 '16 at 17:55

0 Answers0