make list as nested list with consecutive elements in separate list
I want to separate list of elements into nested list, each sub list having consecutive elements. If an element doesn't have a consecutive element, it should have in single list.
Input:
l1 = [1, 2, 3, 11, 12, 13, 23, 33, 34, 35, 45]
l2 = [11, 12, 13, 22, 23, 24, 33, 34]
l3 = [1, 2, 3, 11, 12, 13, 32, 33, 34, 45]
expected output:
l1 = [[1, 2, 3], [11, 12, 13], [23], [33, 34, 35], [45]]
l2 = [[11, 12, 13], [22, 23, 24], [33, 34]]
l3 = [[1, 2, 3], [11, 12, 13], [32, 33, 34], [45]]
I have tried the code below but it is not giving the expected result, printing an empty list:
def split_into_list(l):
t = []
for i in range(len(l) - 1):
if abs(l[i] - l[i + 1]) == 0:
t.append(l[i])
elif abs(l[i] - l[i + 1]) != 0 and abs(l[i - 1] - l[i]) == 0:
t.append(l[i])
yield t
split_into_list(l[i:])
if i + 1 == len(l):
t.append(l[i])
yield t
l = [1, 2, 3, 11, 12, 13, 32, 33, 34, 45]
li = []
li.append(split_into_list(l))
for i in li:
print(i, list(i))
Answer
Your approach seems to have a few issues in the logic that prevent it from working as expected. Specifically:
- The condition
if abs(l[i] - l[i + 1]) == 0:
doesn't capture consecutive numbers as intended. - The recursive call and
yield
within the loop could lead to premature termination or incorrect output. - You don't need recursion here, and the function should accumulate consecutive elements until a gap is found.
Correct Approach:
To achieve the desired output, you need to:
- Iterate through the list, checking if each element is consecutive to the next one.
- When a non-consecutive element is found, start a new sublist.
- Ensure that the last element in the list is also included, even if it doesn't have a consecutive element.
Here is an updated solution:
def split_into_list(l):
result = []
current_group = []
for i in range(len(l)):
if not current_group: # Start a new group
current_group.append(l[i])
elif l[i] == l[i - 1] + 1: # Check if consecutive
current_group.append(l[i])
else:
result.append(current_group) # Add the current group to result
current_group = [l[i]] # Start a new group
if current_group: # Add the last group
result.append(current_group)
return result
# Test with different lists
l1 = [1, 2, 3, 11, 12, 13, 23, 33, 34, 35, 45]
l2 = [11, 12, 13, 22, 23, 24, 33, 34]
l3 = [1, 2, 3, 11, 12, 13, 32, 33, 34, 45]
print(split_into_list(l1)) # Expected: [[1, 2, 3], [11, 12, 13], [23], [33, 34, 35], [45]]
print(split_into_list(l2)) # Expected: [[11, 12, 13], [22, 23, 24], [33, 34]]
print(split_into_list(l3)) # Expected: [[1, 2, 3], [11, 12, 13], [32, 33, 34], [45]]
Explanation:
- Loop through the list: Start with an empty
current_group
list. Iterate over the input list and keep adding consecutive elements tocurrent_group
. - Check for Consecutiveness: If the current element
l[i]
is consecutive to the previous one (l[i - 1] + 1
), add it to thecurrent_group
. - Add a New Group: When a gap is found (i.e., non-consecutive element), append the current group to
result
, and start a new group with the current element. - End of the List: After the loop, don't forget to append the final group.
Output:
[[1, 2, 3], [11, 12, 13], [23], [33, 34, 35], [45]]
[[11, 12, 13], [22, 23, 24], [33, 34]]
[[1, 2, 3], [11, 12, 13], [32, 33, 34], [45]]
This gives you the expected result where consecutive elements are grouped together, and non-consecutive elements are placed in separate sublists.