Home > Blockchain >  how to get nth chunks of items of a list in python?
how to get nth chunks of items of a list in python?

Time:04-14

I would like to get every 4 chunks of a list of items, where the first item has the index corresponding to 4-1, so a previous step. I am only able to get every 4 chunks, but I am stuck at getting every item of the list to start at a "previous step" or 4-1.

Should I loop through this differently?

Current code:

l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']

four_chunks = [l[x:x 4] for x in range(0, len(l), 4)]

##output of four_chunks: 
[['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']]


Desired output:

[['a', 'b', 'c', 'd'], ['d', 'e', 'f', 'g'], ['g', 'h', 'i', 'j']]

As you can see in the desired output, every chunk begins with an item that ended the previous chunk. E.g. item [1] starts with 'd' rather than 'e'.

CodePudding user response:

As Veedrac and guorui said, you need to pick 3 as step parameter of range.

four_chunks = [l[x:x 4] for x in range(0, len(l), 3)]
# or
four_chunks = [l[x:x 4] for x in range(0, len(l) - 3, 3)]

What's the difference? In case your list cannot be split on equal chunks (so len(l) % 3 != 1) The latter will cut last chunk, the former will have last chunk with size < 4

CodePudding user response:

i got interested in the question and made an iterator-based solution. i borrowed a lot from pairwise and others.

this is what i came up with:

from itertools import tee, islice

def overlapping_chunks(iterable, n):
    iters = tuple(it for _, it in (tee(iterable) for _ in range(n)))
    # advance iters by i
    for i, it in enumerate(iters):
        next(islice(it, i, i), None)
    # make all iters step by n-1
    return zip(*(islice(it, None, None, n - 1) for it in iters))

applied to your list:

l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
print(list(overlapping_chunks(iterable=l, n=4)))
# -> [('a', 'b', 'c', 'd'), ('d', 'e', 'f', 'g'), ('g', 'h', 'i', 'j')]

this has the (small) advantage that you do not have to create the whole list if you iterate over the parts:

for part in overlapping_chunks(iterable=l, n=4):
    print(part)
  • Related