How to create subelements with conditions in list?

Question:

I have a list like this

a = [3, 2, 1, 2, 3, 1, 3, 1, 2]

I’m trying to get a new list with elements separated in sublist for following conditions:

1-) Separate in sublists the sequences from 1 to N (N in this case is 3)

2-) Separate in sublists the sequences from 1 to N even there are missing numbers between 1 and N

3-) Separate in sublists the rest elements

The expected output would be like this.

b = [[3], [2], [1, 2, 3], [1, 3], [1, 2]]

My current attempt is below but is associating incorrectly the elements. Thanks for any help

b = [None]*len(a)
for i in range(len(a)):
    if 1 not in a[i][0]:
        b[i] = [a[i]] 
Asked By: Rasec Malkic

||

Answers:

Hope this helps you:

result = []
last_1_index = None
current_sublist = []

for index, element in enumerate(a):
    if element == 1:
        last_1_index = index

        # Create an instance of list and store its reference.
        # This helps you to keep adding value to the list
        # as long as you don't re-assign the current sublist value

        current_sublist = [1]
        result.append(current_sublist)
        continue

    else:
        no_1_found_yet = last_1_index is None
        if no_1_found_yet:
            case_3 = [element]
            result.append(case_3)
            continue
        current_sublist.append(element)
Answered By: Ananta

You can add a new sub-list to the output when the current item is not greater than the last item of the last sub-list of the output:

a = [3, 2, 1, 2, 3, 1, 3, 1, 2]
n = 3

output = []
for i in a:
    if not output or not 1 <= output[-1][-1] < i <= n:
        output.append([])
    output[-1].append(i)

output becomes:

[[3], [2], [1, 2, 3], [1, 3], [1, 2]]

Demo: https://replit.com/@blhsing/FavoriteLargeSite

Answered By: blhsing

To understand the logic in a simple way

#YOUR LIST
a = [3, 2, 1, 2, 3, 1, 3, 1, 2]

#YOUR LIST OF LISTS
finalList = [] 

#TEMPORAY LIST
tempList = []
size = len(a)

#LOGIC
for i in range(len(a)):
    if (i+1 < size) and  (a[i] < a[i+1]):
        tempList.append(a[i])
    else:
        tempList.append(a[i])
        finalList.append(tempList[:]) #NO REFERENCE.
        tempList.clear()

print(finalList)
Answered By: Pavan Chandaka

Here’s the initial version that I think solves the problem properly. He should still be able to simplify, but at least his answer will always be correct

a = [3, 2, 1, 2, 3, 4, 3, 2, 2, 3, 2, 1, 3, 1, 2]
a = "".join(str(i) for i in a)
a = ["1" + i if index > 0 else i for index, i in enumerate(a.split("1"))]
result = []
for i in a:
    if i[0] != "1":
        for j in i:
            result.append([int(j)])
    else:
        max_number_index = i.find(str(max(i)))
        result.append([int(_) for _ in i[: max_number_index + 1]])
        result.extend([[int(s)] for s in i[max_number_index:]])
print(result)
Answered By: 911
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.