Split lists into chunk based of index of another list

半世苍凉 提交于 2019-12-22 10:38:53

问题


I want to split a list into chunks using values of of another list as the range to split.

indices = [3, 5, 9, 13, 18]
my_list = ['a', 'b', 'c', ..., 'x', 'y', 'z']

So basically, split my_list from range:

my_list[:3], mylist[3:5], my_list[5:9], my_list[9:13], my_list[13:18], my_list[18:]

I have tried to indices into chunks of 2 but the result is not what i need.

[indices[i:i + 2] for i in range(0, len(indices), 2)]

My actual list length is 1000.


回答1:


You could also do it using simple python.

Data

indices = [3, 5, 9, 13, 18]
my_list = list('abcdefghijklmnopqrstuvwxyz')

Solution

Use list comprehension.

[my_list[slice(ix,iy)] for ix, iy in zip([0]+indices, indices+[-1])]

Output

[['a', 'b', 'c'],
 ['d', 'e'],
 ['f', 'g', 'h', 'i'],
 ['j', 'k', 'l', 'm'],
 ['n', 'o', 'p', 'q', 'r'],
 ['s', 't', 'u', 'v', 'w', 'x', 'y']]

Check if correct order of indices are extracted

dict(((ix,iy), my_list[slice(ix,iy)]) for ix, iy in zip([0]+indices, indices+[-1]))

Output

{(0, 3): ['a', 'b', 'c'],
 (3, 5): ['d', 'e'],
 (5, 9): ['f', 'g', 'h', 'i'],
 (9, 13): ['j', 'k', 'l', 'm'],
 (13, 18): ['n', 'o', 'p', 'q', 'r'],
 (18, -1): ['s', 't', 'u', 'v', 'w', 'x', 'y']}



回答2:


One way using itertools.tee and pairwise:

from itertools import tee

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

chunks = [my_list[i:j] for i, j in pairwise([0, *indices, len(my_list)])]
print(chunks)

Output:

[['a', 'b', 'c'],
 ['d', 'e'],
 ['f', 'g', 'h', 'i'],
 ['j', 'k', 'l', 'm'],
 ['n', 'o', 'p', 'q', 'r'],
 ['s', 't', 'u', 'v', 'w', 'x', 'y', 'z']]

If numpy is an option, use numpy.array_split, which is meant for this:

import numpy as np

np.array_split(my_list, indices)

Output:

[array(['a', 'b', 'c'], dtype='<U1'),
 array(['d', 'e'], dtype='<U1'),
 array(['f', 'g', 'h', 'i'], dtype='<U1'),
 array(['j', 'k', 'l', 'm'], dtype='<U1'),
 array(['n', 'o', 'p', 'q', 'r'], dtype='<U1'),
 array(['s', 't', 'u', 'v', 'w', 'x', 'y', 'z'], dtype='<U1')]



回答3:


Can use itertools.zip_longest

[my_list[a:b] for a,b in it.zip_longest([0]+indices, indices)]

[['a', 'b', 'c'],
 ['d', 'e'],
 ['f', 'g', 'h', 'i'],
 ['j', 'k', 'l', 'm'],
 ['n', 'o', 'p', 'q', 'r'],
 ['s', 't', 'u', 'v', 'x', 'y', 'z']]

A little bit of code golf for fun:

map(my_list.__getitem__, map(lambda s: slice(*s), it.zip_longest([0]+indices, indices)))


来源:https://stackoverflow.com/questions/57916019/split-lists-into-chunk-based-of-index-of-another-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!