1

Given a list of integers A=[1,2,3,4,5,6,7,9] and a list p=[4,5,9] , how can I separate the values in A so that if they do not appear in p, they will be separated into a sub-list dictated by the position of the element of p in A. For example, in this case the output should be A=[[1, 2, 3], 4, 5, [6, 7, 8], 9].

s=25
# make it a string
s = str(s)

output = []
last = None

for c in A:
    if last is None:
        output.append(c)
    elif (last in s) == (c in s):
        output[-1] = output[-1] + c
    else:
        output.append(c)
    last = c

output # ['1', '2', '34', '5', '67']

This is a similar version of the problem that involves a list of strings.

Reference : Joining elements in Python list

4
  • Hey there, have you written any code to attempt this, that we can look at? Commented Jan 21, 2020 at 17:27
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Jan 21, 2020 at 17:32
  • I saw a similar question on: stackoverflow.com/questions/59813650/… and was interested in playing around with this idea Commented Jan 21, 2020 at 17:33
  • 1
    It would be really helpful if you could show what you have coded so far. Edit your question to include it before this is getting closed. Commented Jan 21, 2020 at 17:35

2 Answers 2

2

You could do this by keeping track of all the elements found so far, and resetting the temporary list when you find an element in p:

A=[1,2,3,4,5,6,7,9]
p=[4,5,9]

def join_elements(A, p):
    curr = []
    for i in A: # Loop over possible values
        if i in p:
            if curr: # yield and reset current list
                yield curr 
                curr = []
            yield i # yield value from p 
        else: # Add to current list
            curr.append(i)
    if curr: # if we ended with an element that was not in p
        yield curr

print(list(join_elements(A, p)))

# [[1, 2, 3], 4, 5, [6, 7], 9]
Sign up to request clarification or add additional context in comments.

1 Comment

@Newbie123 Updated my answer.
0

Here you could find a general solution:

sub_list = [4, 5, 9]

temp = []
result = []
for i in range(1, 10):

    if i not in sub_list:
        temp.append(i)
        if temp not in result:
            result.append(temp)

    else:
        if len(temp) != 0:
            temp = []
        result.append(i)

print(result)



Output: [[1, 2, 3], 4, 5, [6, 7, 8], 9]

I believe this is what you are looking for.

2 Comments

Shouldn't it be for i in A? (A is not guaranteed to be range(10)). Also, this fails if A ends with an element not in p.
Thanks @rassar - I have made some corrections and I believe it would work for all types of list. Please correct me if still there is any scope of improvement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.