Slicing a list into sublists based on condition [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
This question already has an answer here:
Group consecutive integers and tolerate gaps of 1
6 answers
I want to slice this list of numbers:
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
into multiple sublists. The condition for slicing is that the numbers in each sublist should be in increasing order.
So the final result will look like this:
list_1 = [97, 122]
list_2 = [99]
list_3 = [98, 111, 112, 113]
list_4 = [100, 102]
Can anyone help me to solve this problem please? Thanks a lot
python python-3.x list
marked as duplicate by Kasrâmvd
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Sep 28 at 11:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
9
down vote
favorite
This question already has an answer here:
Group consecutive integers and tolerate gaps of 1
6 answers
I want to slice this list of numbers:
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
into multiple sublists. The condition for slicing is that the numbers in each sublist should be in increasing order.
So the final result will look like this:
list_1 = [97, 122]
list_2 = [99]
list_3 = [98, 111, 112, 113]
list_4 = [100, 102]
Can anyone help me to solve this problem please? Thanks a lot
python python-3.x list
marked as duplicate by Kasrâmvd
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Sep 28 at 11:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
This question already has an answer here:
Group consecutive integers and tolerate gaps of 1
6 answers
I want to slice this list of numbers:
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
into multiple sublists. The condition for slicing is that the numbers in each sublist should be in increasing order.
So the final result will look like this:
list_1 = [97, 122]
list_2 = [99]
list_3 = [98, 111, 112, 113]
list_4 = [100, 102]
Can anyone help me to solve this problem please? Thanks a lot
python python-3.x list
This question already has an answer here:
Group consecutive integers and tolerate gaps of 1
6 answers
I want to slice this list of numbers:
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
into multiple sublists. The condition for slicing is that the numbers in each sublist should be in increasing order.
So the final result will look like this:
list_1 = [97, 122]
list_2 = [99]
list_3 = [98, 111, 112, 113]
list_4 = [100, 102]
Can anyone help me to solve this problem please? Thanks a lot
This question already has an answer here:
Group consecutive integers and tolerate gaps of 1
6 answers
python python-3.x list
python python-3.x list
edited Sep 28 at 8:59
jpp
69.3k174085
69.3k174085
asked Sep 28 at 8:34
Huy Nguyen Bui
775
775
marked as duplicate by Kasrâmvd
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Sep 28 at 11:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Kasrâmvd
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Sep 28 at 11:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
I've quickly written one way to do this, I'm sure there are more efficient ways, but this works at least:
num_list =[97, 122, 99, 98, 111, 112, 113, 100, 102]
arrays = [[num_list[0]]] # array of sub-arrays (starts with first value)
for i in range(1, len(num_list)): # go through each element after the first
if num_list[i - 1] < num_list[i]: # If it's larger than the previous
arrays[len(arrays) - 1].append(num_list[i]) # Add it to the last sub-array
else: # otherwise
arrays.append([num_list[i]]) # Make a new sub-array
print(arrays)
Hopefully this helps you a bit :)
add a comment |Â
up vote
5
down vote
Creating a variable number of variables is not recommended. Use a list of lists or dictionary instead. Here's an example with dict
and a generator function:
from itertools import islice, zip_longest
def yield_lists(L):
x =
for i, j in zip_longest(L, islice(L, 1, None), fillvalue=L[-1]):
x.append(i)
if i > j:
yield x
x =
yield x
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
res = dict(enumerate(yield_lists(num_list), 1))
Resut:
1: [97, 122],
2: [99],
3: [98, 111, 112, 113],
4: [100, 102]
For example, access the second list via res[2]
.
2
Ideal solution, generators for the win
â Take_Care_
Sep 28 at 9:01
add a comment |Â
up vote
4
down vote
Here is a one-linear Numpythonic approach:
np.split(arr, np.where(np.diff(arr) < 0)[0] + 1)
Or a similar approach to numpy code but less efficient:
from operator import sub
from itertools import starmap
indices = [0] + [
i+1 for i, j in enumerate(list(
starmap(sub, zip(num_list[1:], num_list)))
) if j < 0] + [len(num_list)
] + [len(num_list)]
result = [num_list[i:j] for i, j in zip(indices, indices[1:])]
Demo:
# Numpy
In [8]: np.split(num_list, np.where(np.diff(num_list) < 0)[0] + 1)
Out[8]:
[array([ 97, 122]),
array([99]),
array([ 98, 111, 112, 113]),
array([100, 102])]
# Python
In [42]: from operator import sub
In [43]: from itertools import starmap
In [44]: indices = [0] + [i+1 for i, j in enumerate(list(starmap(sub, zip(num_list[1:], num_list)))) if j < 0] + [len(num_list)]
In [45]: [num_list[i:j] for i, j in zip(indices, indices[1:])]
Out[45]: [[97, 122], [99], [98, 111, 112, 113], [100, 102]]
Explanation:
Using np.diff()
you can get the differences of each item with their next item (up until the last element). Then you can use the vectorized nature of numpy to get the indices of the places where this difference is negative, which can be done with a simple comparison and np.where()
. Finally you can simply pass the indices to np.split()
to split the array based on those indices.
Out of curiosity, what would be the last element of thenp.diff(num_list)
array? Because the last element has no next item.
â Guimoute
Sep 28 at 9:02
@Guimoute There's no diff for last element. Just added that to the answer.
â Kasrâmvd
Sep 28 at 9:03
add a comment |Â
up vote
2
down vote
All nice solutions here. Maybe this one will be easier to understand for some ppl?
def increasing(a, b):
return a < b
def seq_split(lst, cond):
sublst = [lst[0]]
for item in lst[1:]:
if cond(sublst[-1], item):
sublst.append(item)
else:
yield sublst
sublst = [item]
if sublst:
yield sublst
list(seq_split(num_list, increasing))
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I've quickly written one way to do this, I'm sure there are more efficient ways, but this works at least:
num_list =[97, 122, 99, 98, 111, 112, 113, 100, 102]
arrays = [[num_list[0]]] # array of sub-arrays (starts with first value)
for i in range(1, len(num_list)): # go through each element after the first
if num_list[i - 1] < num_list[i]: # If it's larger than the previous
arrays[len(arrays) - 1].append(num_list[i]) # Add it to the last sub-array
else: # otherwise
arrays.append([num_list[i]]) # Make a new sub-array
print(arrays)
Hopefully this helps you a bit :)
add a comment |Â
up vote
2
down vote
accepted
I've quickly written one way to do this, I'm sure there are more efficient ways, but this works at least:
num_list =[97, 122, 99, 98, 111, 112, 113, 100, 102]
arrays = [[num_list[0]]] # array of sub-arrays (starts with first value)
for i in range(1, len(num_list)): # go through each element after the first
if num_list[i - 1] < num_list[i]: # If it's larger than the previous
arrays[len(arrays) - 1].append(num_list[i]) # Add it to the last sub-array
else: # otherwise
arrays.append([num_list[i]]) # Make a new sub-array
print(arrays)
Hopefully this helps you a bit :)
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I've quickly written one way to do this, I'm sure there are more efficient ways, but this works at least:
num_list =[97, 122, 99, 98, 111, 112, 113, 100, 102]
arrays = [[num_list[0]]] # array of sub-arrays (starts with first value)
for i in range(1, len(num_list)): # go through each element after the first
if num_list[i - 1] < num_list[i]: # If it's larger than the previous
arrays[len(arrays) - 1].append(num_list[i]) # Add it to the last sub-array
else: # otherwise
arrays.append([num_list[i]]) # Make a new sub-array
print(arrays)
Hopefully this helps you a bit :)
I've quickly written one way to do this, I'm sure there are more efficient ways, but this works at least:
num_list =[97, 122, 99, 98, 111, 112, 113, 100, 102]
arrays = [[num_list[0]]] # array of sub-arrays (starts with first value)
for i in range(1, len(num_list)): # go through each element after the first
if num_list[i - 1] < num_list[i]: # If it's larger than the previous
arrays[len(arrays) - 1].append(num_list[i]) # Add it to the last sub-array
else: # otherwise
arrays.append([num_list[i]]) # Make a new sub-array
print(arrays)
Hopefully this helps you a bit :)
answered Sep 28 at 8:44
DeltaMarine101
5611319
5611319
add a comment |Â
add a comment |Â
up vote
5
down vote
Creating a variable number of variables is not recommended. Use a list of lists or dictionary instead. Here's an example with dict
and a generator function:
from itertools import islice, zip_longest
def yield_lists(L):
x =
for i, j in zip_longest(L, islice(L, 1, None), fillvalue=L[-1]):
x.append(i)
if i > j:
yield x
x =
yield x
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
res = dict(enumerate(yield_lists(num_list), 1))
Resut:
1: [97, 122],
2: [99],
3: [98, 111, 112, 113],
4: [100, 102]
For example, access the second list via res[2]
.
2
Ideal solution, generators for the win
â Take_Care_
Sep 28 at 9:01
add a comment |Â
up vote
5
down vote
Creating a variable number of variables is not recommended. Use a list of lists or dictionary instead. Here's an example with dict
and a generator function:
from itertools import islice, zip_longest
def yield_lists(L):
x =
for i, j in zip_longest(L, islice(L, 1, None), fillvalue=L[-1]):
x.append(i)
if i > j:
yield x
x =
yield x
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
res = dict(enumerate(yield_lists(num_list), 1))
Resut:
1: [97, 122],
2: [99],
3: [98, 111, 112, 113],
4: [100, 102]
For example, access the second list via res[2]
.
2
Ideal solution, generators for the win
â Take_Care_
Sep 28 at 9:01
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Creating a variable number of variables is not recommended. Use a list of lists or dictionary instead. Here's an example with dict
and a generator function:
from itertools import islice, zip_longest
def yield_lists(L):
x =
for i, j in zip_longest(L, islice(L, 1, None), fillvalue=L[-1]):
x.append(i)
if i > j:
yield x
x =
yield x
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
res = dict(enumerate(yield_lists(num_list), 1))
Resut:
1: [97, 122],
2: [99],
3: [98, 111, 112, 113],
4: [100, 102]
For example, access the second list via res[2]
.
Creating a variable number of variables is not recommended. Use a list of lists or dictionary instead. Here's an example with dict
and a generator function:
from itertools import islice, zip_longest
def yield_lists(L):
x =
for i, j in zip_longest(L, islice(L, 1, None), fillvalue=L[-1]):
x.append(i)
if i > j:
yield x
x =
yield x
num_list = [97, 122, 99, 98, 111, 112, 113, 100, 102]
res = dict(enumerate(yield_lists(num_list), 1))
Resut:
1: [97, 122],
2: [99],
3: [98, 111, 112, 113],
4: [100, 102]
For example, access the second list via res[2]
.
edited Sep 28 at 14:22
answered Sep 28 at 8:52
jpp
69.3k174085
69.3k174085
2
Ideal solution, generators for the win
â Take_Care_
Sep 28 at 9:01
add a comment |Â
2
Ideal solution, generators for the win
â Take_Care_
Sep 28 at 9:01
2
2
Ideal solution, generators for the win
â Take_Care_
Sep 28 at 9:01
Ideal solution, generators for the win
â Take_Care_
Sep 28 at 9:01
add a comment |Â
up vote
4
down vote
Here is a one-linear Numpythonic approach:
np.split(arr, np.where(np.diff(arr) < 0)[0] + 1)
Or a similar approach to numpy code but less efficient:
from operator import sub
from itertools import starmap
indices = [0] + [
i+1 for i, j in enumerate(list(
starmap(sub, zip(num_list[1:], num_list)))
) if j < 0] + [len(num_list)
] + [len(num_list)]
result = [num_list[i:j] for i, j in zip(indices, indices[1:])]
Demo:
# Numpy
In [8]: np.split(num_list, np.where(np.diff(num_list) < 0)[0] + 1)
Out[8]:
[array([ 97, 122]),
array([99]),
array([ 98, 111, 112, 113]),
array([100, 102])]
# Python
In [42]: from operator import sub
In [43]: from itertools import starmap
In [44]: indices = [0] + [i+1 for i, j in enumerate(list(starmap(sub, zip(num_list[1:], num_list)))) if j < 0] + [len(num_list)]
In [45]: [num_list[i:j] for i, j in zip(indices, indices[1:])]
Out[45]: [[97, 122], [99], [98, 111, 112, 113], [100, 102]]
Explanation:
Using np.diff()
you can get the differences of each item with their next item (up until the last element). Then you can use the vectorized nature of numpy to get the indices of the places where this difference is negative, which can be done with a simple comparison and np.where()
. Finally you can simply pass the indices to np.split()
to split the array based on those indices.
Out of curiosity, what would be the last element of thenp.diff(num_list)
array? Because the last element has no next item.
â Guimoute
Sep 28 at 9:02
@Guimoute There's no diff for last element. Just added that to the answer.
â Kasrâmvd
Sep 28 at 9:03
add a comment |Â
up vote
4
down vote
Here is a one-linear Numpythonic approach:
np.split(arr, np.where(np.diff(arr) < 0)[0] + 1)
Or a similar approach to numpy code but less efficient:
from operator import sub
from itertools import starmap
indices = [0] + [
i+1 for i, j in enumerate(list(
starmap(sub, zip(num_list[1:], num_list)))
) if j < 0] + [len(num_list)
] + [len(num_list)]
result = [num_list[i:j] for i, j in zip(indices, indices[1:])]
Demo:
# Numpy
In [8]: np.split(num_list, np.where(np.diff(num_list) < 0)[0] + 1)
Out[8]:
[array([ 97, 122]),
array([99]),
array([ 98, 111, 112, 113]),
array([100, 102])]
# Python
In [42]: from operator import sub
In [43]: from itertools import starmap
In [44]: indices = [0] + [i+1 for i, j in enumerate(list(starmap(sub, zip(num_list[1:], num_list)))) if j < 0] + [len(num_list)]
In [45]: [num_list[i:j] for i, j in zip(indices, indices[1:])]
Out[45]: [[97, 122], [99], [98, 111, 112, 113], [100, 102]]
Explanation:
Using np.diff()
you can get the differences of each item with their next item (up until the last element). Then you can use the vectorized nature of numpy to get the indices of the places where this difference is negative, which can be done with a simple comparison and np.where()
. Finally you can simply pass the indices to np.split()
to split the array based on those indices.
Out of curiosity, what would be the last element of thenp.diff(num_list)
array? Because the last element has no next item.
â Guimoute
Sep 28 at 9:02
@Guimoute There's no diff for last element. Just added that to the answer.
â Kasrâmvd
Sep 28 at 9:03
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Here is a one-linear Numpythonic approach:
np.split(arr, np.where(np.diff(arr) < 0)[0] + 1)
Or a similar approach to numpy code but less efficient:
from operator import sub
from itertools import starmap
indices = [0] + [
i+1 for i, j in enumerate(list(
starmap(sub, zip(num_list[1:], num_list)))
) if j < 0] + [len(num_list)
] + [len(num_list)]
result = [num_list[i:j] for i, j in zip(indices, indices[1:])]
Demo:
# Numpy
In [8]: np.split(num_list, np.where(np.diff(num_list) < 0)[0] + 1)
Out[8]:
[array([ 97, 122]),
array([99]),
array([ 98, 111, 112, 113]),
array([100, 102])]
# Python
In [42]: from operator import sub
In [43]: from itertools import starmap
In [44]: indices = [0] + [i+1 for i, j in enumerate(list(starmap(sub, zip(num_list[1:], num_list)))) if j < 0] + [len(num_list)]
In [45]: [num_list[i:j] for i, j in zip(indices, indices[1:])]
Out[45]: [[97, 122], [99], [98, 111, 112, 113], [100, 102]]
Explanation:
Using np.diff()
you can get the differences of each item with their next item (up until the last element). Then you can use the vectorized nature of numpy to get the indices of the places where this difference is negative, which can be done with a simple comparison and np.where()
. Finally you can simply pass the indices to np.split()
to split the array based on those indices.
Here is a one-linear Numpythonic approach:
np.split(arr, np.where(np.diff(arr) < 0)[0] + 1)
Or a similar approach to numpy code but less efficient:
from operator import sub
from itertools import starmap
indices = [0] + [
i+1 for i, j in enumerate(list(
starmap(sub, zip(num_list[1:], num_list)))
) if j < 0] + [len(num_list)
] + [len(num_list)]
result = [num_list[i:j] for i, j in zip(indices, indices[1:])]
Demo:
# Numpy
In [8]: np.split(num_list, np.where(np.diff(num_list) < 0)[0] + 1)
Out[8]:
[array([ 97, 122]),
array([99]),
array([ 98, 111, 112, 113]),
array([100, 102])]
# Python
In [42]: from operator import sub
In [43]: from itertools import starmap
In [44]: indices = [0] + [i+1 for i, j in enumerate(list(starmap(sub, zip(num_list[1:], num_list)))) if j < 0] + [len(num_list)]
In [45]: [num_list[i:j] for i, j in zip(indices, indices[1:])]
Out[45]: [[97, 122], [99], [98, 111, 112, 113], [100, 102]]
Explanation:
Using np.diff()
you can get the differences of each item with their next item (up until the last element). Then you can use the vectorized nature of numpy to get the indices of the places where this difference is negative, which can be done with a simple comparison and np.where()
. Finally you can simply pass the indices to np.split()
to split the array based on those indices.
edited Sep 28 at 9:52
answered Sep 28 at 8:57
Kasrâmvd
76k1086118
76k1086118
Out of curiosity, what would be the last element of thenp.diff(num_list)
array? Because the last element has no next item.
â Guimoute
Sep 28 at 9:02
@Guimoute There's no diff for last element. Just added that to the answer.
â Kasrâmvd
Sep 28 at 9:03
add a comment |Â
Out of curiosity, what would be the last element of thenp.diff(num_list)
array? Because the last element has no next item.
â Guimoute
Sep 28 at 9:02
@Guimoute There's no diff for last element. Just added that to the answer.
â Kasrâmvd
Sep 28 at 9:03
Out of curiosity, what would be the last element of the
np.diff(num_list)
array? Because the last element has no next item.â Guimoute
Sep 28 at 9:02
Out of curiosity, what would be the last element of the
np.diff(num_list)
array? Because the last element has no next item.â Guimoute
Sep 28 at 9:02
@Guimoute There's no diff for last element. Just added that to the answer.
â Kasrâmvd
Sep 28 at 9:03
@Guimoute There's no diff for last element. Just added that to the answer.
â Kasrâmvd
Sep 28 at 9:03
add a comment |Â
up vote
2
down vote
All nice solutions here. Maybe this one will be easier to understand for some ppl?
def increasing(a, b):
return a < b
def seq_split(lst, cond):
sublst = [lst[0]]
for item in lst[1:]:
if cond(sublst[-1], item):
sublst.append(item)
else:
yield sublst
sublst = [item]
if sublst:
yield sublst
list(seq_split(num_list, increasing))
add a comment |Â
up vote
2
down vote
All nice solutions here. Maybe this one will be easier to understand for some ppl?
def increasing(a, b):
return a < b
def seq_split(lst, cond):
sublst = [lst[0]]
for item in lst[1:]:
if cond(sublst[-1], item):
sublst.append(item)
else:
yield sublst
sublst = [item]
if sublst:
yield sublst
list(seq_split(num_list, increasing))
add a comment |Â
up vote
2
down vote
up vote
2
down vote
All nice solutions here. Maybe this one will be easier to understand for some ppl?
def increasing(a, b):
return a < b
def seq_split(lst, cond):
sublst = [lst[0]]
for item in lst[1:]:
if cond(sublst[-1], item):
sublst.append(item)
else:
yield sublst
sublst = [item]
if sublst:
yield sublst
list(seq_split(num_list, increasing))
All nice solutions here. Maybe this one will be easier to understand for some ppl?
def increasing(a, b):
return a < b
def seq_split(lst, cond):
sublst = [lst[0]]
for item in lst[1:]:
if cond(sublst[-1], item):
sublst.append(item)
else:
yield sublst
sublst = [item]
if sublst:
yield sublst
list(seq_split(num_list, increasing))
answered Sep 28 at 9:28
code22
1025
1025
add a comment |Â
add a comment |Â