Python: split list into indices based on consecutive identical values
<p>If you could advice me how to write the script to split list by number of values I mean: </p>

<pre><code>my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 
</code></pre>

<p>And there are 11-4,12-2,15-6,20-3 items.
 So in next list for exsample range(0:100) 
I have to split on 4,2,6,3 parts 
So I counted same values and function for split list, but it doen't work with list:</p>

<pre><code> div=Counter(my_list).values() ##counts same values in the list

 def chunk(it, size):
    it = iter(it)
    return iter(lambda: tuple(islice(it, size)), ())
</code></pre>

<p>What do I need:</p>

<pre><code>Out: ([0,1,2,3],[4,5],[6,7,8,9,10,11], etc...] 
</code></pre>

<p>Solution in Python 3 , If you are only using <code>counter</code> :</p>

<pre><code>from collections import Counter
my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 
count = Counter(my_list)
div= list(count.keys())         # take only keys
div.sort()
l = []
num = 0
for i in div:
    t = []
    for j in range(count[i]):   # loop number of times it occurs in the list
        t.append(num)
        num+=1
    l.append(t)
print(l)
</code></pre>

<p>Output:</p>

<pre><code>[[0, 1, 2, 3], [4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14]]
</code></pre>

<p>Alternate Solution using <code>set</code>:</p>

<pre><code>my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 
val = set(my_list)                     # filter only unique elements
ans = []
num = 0
for i in val:
    temp = []
    for j in range(my_list.count(i)):   # loop till number of occurrence of each unique element
        temp.append(num)
        num+=1
    ans.append(temp)
print(ans)
</code></pre>

<p><strong>EDIT:</strong>
As per required changes made to get desired output as mention in comments by @Protoss Reed</p>

<pre><code>my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 
val = list(set(my_list))                     # filter only unique elements
val.sort()                                   # because set is not sorted by default
ans = []
index = 0
l2 = [54,21,12,45,78,41,235,7,10,4,1,1,897,5,79]
for i in val:
    temp = []
    for j in range(my_list.count(i)):   # loop till number of occurrence of each unique element
        temp.append(l2[index])
        index+=1
    ans.append(temp)
print(ans)
</code></pre>

<p>Output:</p>

<pre><code>[[54, 21, 12, 45], [78, 41], [235, 7, 10, 4, 1, 1], [897, 5, 79]]
</code></pre>

<p>Here I have to convert <code>set</code> into <code>list</code> because <code>set</code> is not sorted and I think remaining is self explanatory.</p>

<p><strong>Another Solution</strong> if input is not always Sorted (using <code>OrderedDict</code>):</p>

<pre><code>from collections import OrderedDict
v = OrderedDict({})
my_list=[12,12,11,11,11,11,20,20,20,15,15,15,15,15,15]
l2 = [54,21,12,45,78,41,235,7,10,4,1,1,897,5,79]
for i in my_list:                # maintain count in dict
    if i in v:
        v[i]+=1
    else:
        v[i]=1
ans =[]
index = 0
for key,values in v.items():
    temp = []
    for j in range(values):
        temp.append(l2[index])
        index+=1
    ans.append(temp)
print(ans)
</code></pre>

<p>Output:</p>

<pre><code>[[54, 21], [12, 45, 78, 41], [235, 7, 10], [4, 1, 1, 897, 5, 79]]
</code></pre>

<p>Here I use <code>OrderedDict</code> to maintain order of input sequence which is random(unpredictable) in case of <code>set</code>.</p>

<p>Although I prefer @Ami Tavory's solution which is more pythonic.</p>

<p>[Extra work: If anybody can convert this solution into <code>list comprehension</code> it will be awesome because i tried but can not convert it to <code>list comprehension</code> and if you succeed please post it in comments it will help me to understand]</p>

python list split