Updated 2022-05-21 1
Viewed 8 times
0

I have the following:

some_list = list(itertools.accumulate(map(len, some_list)))
print([0] + list(itertools.chain.from_iterable(zip(some_list, some_list[1:]))))

which produces:

[0, 343, 807, 807, 2768, 2768, ...]

Is there a way to produces a tuple inside of a list instead of a list of integers?

(0, 343), (807, 807)...

some_list:

import numpy as np

some_list = [
    np.array([1, 2, 3, 4, 5, 6]).reshape(3, 2),
    np.array([7, 8, 9, 10]).reshape(2, 2),
]

# desired result:
# [(0, 3) (3, 2)]

🟢 Solution
1

If you can't use itertools.pairwise, simply zip the shifted list:

import itertools
some_list = [[1],[1,1,1],[1,1,1,1,1]]
some_list = list(itertools.accumulate(map(len, some_list)))
list(zip([0]+some_list, some_list))

Output: [(0, 1), (1, 4), (4, 9)]