Home > Net >  Slicing Python List with inconsistent intervals
Slicing Python List with inconsistent intervals

Time:11-15

I have a list of stock prices of a company. Now I want to split the list with multiple intervals. we will store the price like: The first 2 elements, then next 3 elements, then 2 elements, and so on.

meta_stocks = [10, 9, 11, 15, 19, 22, 25, 11, 15, 17]

Output

meta_stocks = [[10, 9],[11, 15, 19],[22, 25],[ 11, 15, 17]]

I am able to split the list with 5 items each but not able to split it further

>>> [meta_stocks[i:i interval2] for i in range(0, len(meta_stocks), interval2)]
>>> [[10, 9, 11, 15, 19], [22, 25, 11, 15, 17]]

CodePudding user response:

You can use a list comprehension with the help of itertools.cycle:

meta_stocks = [10, 9, 11, 15, 19, 22, 25, 11, 15, 17]

from itertools import cycle

start = 0
l = [2,3]
c = cycle(l)

[meta_stocks[start:(start:=start next(c))]
 for i in range(len(l)*len(meta_stocks)//sum(l))]

Output:

[[10, 9], [11, 15, 19], [22, 25], [11, 15, 17]]

CodePudding user response:

You could do it like this without the aid of additional imports:

meta_stocks = [10, 9, 11, 15, 19, 22, 25, 11, 15, 17]
meta_stocks_out = []
offset = 0
interval = 2
while offset   interval <= len(meta_stocks):
    meta_stocks_out.append(meta_stocks[offset:offset interval])
    offset  = interval
    interval = 2 if interval == 3 else 3
print(meta_stocks_out)

CodePudding user response:

Some itertools to the rescue:

from itertools import islice, cycle, takewhile

i = iter(meta_stocks)
intervals = [2, 3]

[*takewhile(lambda _: _, ([*islice(i, n)] for n in cycle(intervals)))]
# [[10, 9], [11, 15, 19], [22, 25], [11, 15, 17]]

CodePudding user response:

Using itertools.islice

from itertools import islice

meta_stocks = [10, 9, 11, 15, 19, 22, 25, 11, 15, 17]
chunks = [2, 3, 2, 3                 # length of each sublist

it = iter(meta_stocks)               
result = [list(islice(it, i)) for i in chunks]

print(result)
#Output: [[10, 9], [11, 15, 19], [22, 25], [11, 15, 17]]

CodePudding user response:

you can try this First we get a list where each element is list with two sublists of 2 and 3 elements. After that we flatten the 1st level of the list.

import pprint
meta_stocks = [10, 9, 11, 15, 19, 22, 25, 11, 15, 17]
new_list = [item for sublist in [[meta_stocks[i:i 2], meta_stocks[i 2:i 5]] for i in range(0, len(meta_stocks), 5)] for item in sublist]
pprint.pprint(new_list)

and the output

[[10, 9], [11, 15, 19], [22, 25], [11, 15, 17]]

CodePudding user response:

Let's assume you have your intervals expressed a list of integers

meta_stocks = [10, 9, 11, 15, 19, 22, 25, 11, 15, 17]
intervals = [2,3,2,3]

cintervals = [sum(intervals[:i]) for i in range(0,len(intervals) 1)]

sliced = [meta_stocks[cintervals[i-1]:cintervals[i]] for i in range(1, len(cintervals))]

CodePudding user response:

Try:

meta_stocks = [10, 9, 11, 15, 19, 22, 25, 11, 15, 17]

res = [x for i in range(0, len(meta_stocks), 5) for x in [meta_stocks[i:i 2], meta_stocks[i 2:i 5]]]
print(res)

Output

[[10, 9], [11, 15, 19], [22, 25], [11, 15, 17]]

A general approach using itertools.accumulate:

from itertools import accumulate

meta_stocks = [10, 9, 11, 15, 19, 22, 25, 11, 15, 17]

step = sum([2, 3])
steps = list(accumulate([0, 2, 3]))

res = [x for i in range(0, len(meta_stocks), 5) for x in [meta_stocks[i s:i e] for s, e in zip(steps, steps[1:])]]
print(res)

                                                                                                       
  • Related