Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slicing a list and group

Tags:

python

list

For example, if our source list is:

input = [1, 2, 3, 4, 5, 6, 7, 8, 9, ... ]

And I need something like this:

output = {1:[1], 2:[2,3], 3:[4,5,6], 4:[7,8,9,...], ...}

I try like this, but this not work correctly:

groups = {}
N = 1
group = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(0, len(group)-1):
    groups.update({N:group[i:i+N]})
    N+=1
like image 596
TramZzZ Avatar asked Apr 30 '15 19:04

TramZzZ


People also ask

What does [- 1 :] mean in python?

Python also allows you to index from the end of the list using a negative number, where [-1] returns the last element. This is super-useful since it means you don't have to programmatically find out the length of the iterable in order to work with elements at the end of it.

What do you mean by slicing a list?

List slicing refers to accessing a specific portion or a subset of the list for some operation while the orginal list remains unaffected. The slicing operator in python can take 3 parameters out of which 2 are optional depending on the requirement. Syntax of list slicing: list_name[start:stop:steps]

Can we do slicing in list?

As well as using slicing to extract part of a list (i.e. a slice on the right hand sign of an equal sign), you can set the value of elements in a list by using a slice on the left hand side of an equal sign. In python terminology, this is because lists are mutable objects, while strings are immutable.

What is list indexing and slicing with an example?

What are Indexing and Slicing? Indexing: Indexing is used to obtain individual elements. Slicing: Slicing is used to obtain a sequence of elements. Indexing and Slicing can be be done in Python Sequences types like list, string, tuple, range objects.


2 Answers

For completeness sake - you can also write a version which will work on any iterable:

from itertools import islice, count

group = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
res = {k:v for k,v in enumerate(iter(lambda i=iter(group), c=count(1): list(islice(i, next(c))), []), 1)}
# {1: [1], 2: [2, 3], 3: [4, 5, 6], 4: [7, 8, 9, 10]}
like image 125
Jon Clements Avatar answered Nov 22 '22 16:11

Jon Clements


You can use one dict comprehension but first you need to find a proper index range that you could split your elements based on them! for this aim you can use a simple mathematical formula that the sum of a sequence from 1...n is n*(n+1)/2 so in this case n*(n+1)/2=len(l) and with solving the equation you can achieve the n with (1+math.sqrt(1+8*len(l)))/2) :

Some examples :

>>> l=[23,12,33,42,5,6,7,8,39,10,11,102]
>>> ind=range(1,int((1+math.sqrt(1+8*len(l)))/2))
>>> {i:l[sum(ind[:i-1]):sum(ind[:i-1])+i] for i in ind}
{1: [23], 2: [12, 33], 3: [42, 5, 6], 4: [7, 8, 39, 10]}

As the length of 11,102 is not 5 so the n will be 4 in this case! but in following it covers all the elements :

>>> l=[23,12,33,42,5,6,7,8,39,10,11,102,4,0,5]
>>> ind=range(1,int((1+math.sqrt(1+8*len(l)))/2))
>>> {i:l[sum(ind[:i-1]):sum(ind[:i-1])+i] for i in ind}
{1: [23], 2: [12, 33], 3: [42, 5, 6], 4: [7, 8, 39, 10], 5: [11, 102, 4, 0, 5]}

And as a better way you can just calculate the sum(ind[:i-1]) one time :

>>> for i in ind:
...    s=sum(ind[:i-1])
...    d[i]=l[s:s+i]
... 
>>> d
{1: [23], 2: [12, 33], 3: [42, 5, 6], 4: [7, 8, 39, 10], 5: [11, 102, 4, 0, 5]}

Last Note as you can see in first example this solution doesn't keep the last elements if their number doesn't match with the corresponding length. if you want to keep the last elements You can use other answers that are good stuffs!

like image 42
Mazdak Avatar answered Nov 22 '22 17:11

Mazdak