简体   繁体   中英

Take every nth block from list

Given a list:

import string
a = list(string.ascii_lowercase)

What is the Pythonic way to return every nth block of m elements? Note that this is different from just returning every nth element .

Desired result of taking every 1st of 3 blocks of 3 elements (take 3, skip 6, take 3, skip 6...):

['a', 'b', 'c', 'j', 'k', 'l', 's', 't', 'u']

I can get to this as:

import itertools
s1 = a[::9]
s2 = a[1::9]
s3 = a[2::9]    
res = list(itertools.chain.from_iterable(zip(s1,s2, s3)))

Is there a cleaner way?

For a fixed order of select and skip, you can wrap indices taking the modulo on the total length of the window (9 here) and select only those beneath the given threshold, 3:

lst = [x for i, x in enumerate(a) if i % 9 < 3]
print(lst)
# ['a', 'b', 'c', 'j', 'k', 'l', 's', 't', 'u']

You can make this into a function that makes it more intuitive to use:

def select_skip(iterable, select, skip):
    return [x for i, x in enumerate(iterable) if i % (select+skip) < select]  

print(select_skip(a, select=3, skip=6))
# ['a', 'b', 'c', 'j', 'k', 'l', 's', 't', 'u']

Perhaps just writing a simple generator is the most readable

def thinger(iterable, take=3, skip=6):
    it = iter(iterable)
    try:
        while True:
            for i in range(take):
                yield next(it)
            for i in range(skip):
                next(it)
    except StopIteration:
        return

This has the advantage of working even if the input is infinite, or not slicable (eg data coming in from a socket).

more_itertools is a third-party library that implements itertools recipes and other helpful tools such as more_itertools.windowed .

>  pip install more_itertools

Code

import string

from more_itertools import windowed, flatten


m, n = 3, 6
list(flatten(windowed(string.ascii_lowercase, m, step=m+n)))
# ['a', 'b', 'c', 'j', 'k', 'l', 's', 't', 'u']

windowed naturally steps one position per iteration. Given a new step by advancing beyond the overlaps ( m ), the windows are appropriately determined.

You can do it using some generic "chunks" recipe :

windows = chunks(original_iter, n=3)

Now that you've windowed you're data as you think of it, use islice's second variant for its' 'step' capabilities:

# flattens the list as well using chain
result = chain.from_iterable(islice(windows, 0, None, 2))

You can use a list comprehension and create a function that does this for any skip, take and list values:

import string
import itertools
a = list(string.ascii_lowercase)
def everyNthBlock(a, take, skip):
  res = [a[i:i + take] for i in range(0, len(a) ,skip + take)]
  return list(itertools.chain(*res))

print(everyNthBlock(a, 3, 6))
#^^^^ => ['a', 'b', 'c', 'j', 'k', 'l', 's', 't', 'u']
print(everyNthBlock(a, 4, 7))
#^^^^ => ['a', 'b', 'c', 'd', 'l', 'm', 'n', 'o', 'w', 'x', 'y', 'z']

Using incomprehensible list comprehension :D

m, n = 3, 3
[elem for blockstart in range(0, len(a), m*n) for elem in a[blockstart:blockstart+n]]    
#> ['a', 'b', 'c', 'j', 'k', 'l', 's', 't', 'u']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM