Python coding
i want to split list shown below
a=[5,4,2,5,7,5,4,10,2]
if this list is given, i want to split it into
b=[[5,4,2,5],[7,5,4],[10,2]]
the algorithm is split until there is bigger number than 5 then 5,4,2,5 is in one list, next number is 7, so split the list until there is bigger then 7 which is 10. how can i do this?
Advertisement
Answer
arr = [5,4,2,5,7,5,4,10,2]
current = arr[0]
temp = []
res = []
for num in arr:
if num > current:
res.append(temp)
current = num
temp = []
temp.append(num)
res.append(temp)
print(res)
Prints:
[[5, 4, 2, 5], [7, 5, 4], [10, 2]]