Python: Split a List into Multiple Lists Based on a Subset of Elements

Split a python list into other "sublists" i.e smaller lists

I'd say

chunks = [data[x:x+100] for x in range(0, len(data), 100)]

If you are using python 2.x instead of 3.x, you can be more memory-efficient by using xrange(), changing the above code to:

chunks = [data[x:x+100] for x in xrange(0, len(data), 100)]

Python - Split list to multiple lists with respect to a keyword

You can always use list of lists to store the clusters:

clusterlists = []
for i in range(true_k):
dummy_list = []
for ind in order_centroids[i]:
dummy_list.append('%s' % terms[ind])
clusterlists.append(dummy_list)

Here the clusterlists will store clusters and you can reach them with indices.

How to split consecutive elements in a list into sublists

Another way is using more_itertools.consecutive_groups:
(used @Stephen's list for an example):

import more_itertools as mit
for group in mit.consecutive_groups(indices_to_remove):
print(list(group))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
[160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170]

How to split a list into subsets based on a pattern?

You could use itertools.groupby:

>>> import itertools
>>> mylist = ['sub_0_a', 'sub_0_b', 'sub_1_a', 'sub_1_b']
>>> for k,v in itertools.groupby(mylist,key=lambda x:x[:5]):
... print k, list(v)
...
sub_0 ['sub_0_a', 'sub_0_b']
sub_1 ['sub_1_a', 'sub_1_b']

or exactly as you specified it:

>>> [list(v) for k,v in itertools.groupby(mylist,key=lambda x:x[:5])]
[['sub_0_a', 'sub_0_b'], ['sub_1_a', 'sub_1_b']]

Of course, the common caveats apply (Make sure your list is sorted with the same key you're using to group), and you might need a slightly more complicated key function for real world data...

Split a list into sub-lists based on index ranges

Note that you can use a variable in a slice:

l = ['a',' b',' c',' d',' e']
c_index = l.index("c")
l2 = l[:c_index]

This would put the first two entries of l in l2

How to split a list into sublists based on single elements of the initial list first and then contiguous portions of the list simply in python?

Here is a function that finds your desired outcome using a double-loop.

def get_contiguous_sublists(lst):
out = [[]]

# find the length of the input list (added 1 for convenience which will be useful later)
len_lst = len(lst) + 1

# for each integer between 1 and the full length of the input list,
# we slice the input list `lst` to create new lists of this length
# and add it to the output list `out`
for length in range(1, len_lst):
# here, we are changing the starting point of the list slicing,
# i.e. whether we want to start from 1 or 2 or 3 for [1,2,3]
for i in range(len_lst - length):
out += [lst[i : i + length]]
return out

Output:

>>> get_contiguous_sublists([1,2,3])
[[], [1], [2], [3], [1, 2], [2, 3], [1, 2, 3]]


>>> get_contiguous_sublists([1,2,3,4])
[[], [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4], [1, 2, 3, 4]]

How do I split a list into equally-sized chunks?

Here's a generator that yields evenly-sized chunks:

def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
import pprint
pprint.pprint(list(chunks(range(10, 75), 10)))
[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74]]

For Python 2, using xrange instead of range:

def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in xrange(0, len(lst), n):
yield lst[i:i + n]

Below is a list comprehension one-liner. The method above is preferable, though, since using named functions makes code easier to understand. For Python 3:

[lst[i:i + n] for i in range(0, len(lst), n)]

For Python 2:

[lst[i:i + n] for i in xrange(0, len(lst), n)]


Related Topics



Leave a reply



Submit