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