1

I'm trying to separate every continuous segment of consecutive numbers in a different array. For example,

# Input
x=[1,2,3,4,5,8,11,12,13,18]
# Output:
x=[[1,2,3,4,5],[8],[11,12,13],[18]]

The existing code,

x=[1,2,3,4,5,8,11,12,13,18]
temp=[]
firstnumber=0
for i in range(1,len(x)-1,1):
 current=x[i]
 previous=x[i-1]
 if ((current-previous)!=1):
 mm=(x[firstnumber:i-1])
 temp.append(mm)
 firstnumber=x[i]
print(temp)

I only got [[1, 2, 3, 4], []] as a result and I can't figure out why.

ranka47
1,0759 silver badges25 bronze badges
asked Jul 5, 2019 at 18:16
4
  • firstnumber is an element of the list, but you're using it as an index in the mm assigment Commented Jul 5, 2019 at 18:25
  • 2
    In your example [11,12,13,18] is correct? Commented Jul 5, 2019 at 18:26
  • no im expecting it to show x=[[1,2,3,4,5],[8],[11,12,13],[18]] Commented Jul 5, 2019 at 18:34
  • Also see stackoverflow.com/a/2361991/4014959 But note the comments about making it work on Python 3. Commented Jul 6, 2019 at 9:33

4 Answers 4

1

I have tried to answer this question changing as little of your code as possible.

x=[1,2,3,4,5,8,11,12,13,18]
temp=[]
firstnumber=0
first_index = 0
for i in range(1, len(x)):
 current=x[i]
 previous=x[i-1]
 if ((current-previous)!=1):
 mm = x[first_index:i]
 temp.append(mm)
 firstnumber = x[i]
 first_index = i
temp.append(x[first_index:])
print(temp) # [[1, 2, 3, 4, 5], [8], [11, 12, 13], [18]]

What I changed: firstnumber is being used as an index, but in reality is an element of the list, so we need to use first_index = i, the current index on that iteration.

The loop did not cover all the elements of the list, we need to go all the way to the end of the list so we iterate over range(1, len(x)

Finally even if the loop completes it will be missing the last sequence unless we add it after the loop, hence the addition of temp.append(x[first_index:])

NOTE: This method will work with the input you have but it not robust for all cases, nor is it the most efficient way to do this, however, your question was why it did not work as is so hopefully this answers that.

answered Jul 5, 2019 at 18:38
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you so much, just out of curiosity, what would be the most efficient way?
There are a lot of different ways to do this, and depending on the constraints of the problem (is the list always sorted, etc) it's hard to say what the most efficient way is. I can provide an alternate way that is more efficient if you'd like.
1

My answer does not intend to provide repaired code, but rather doing described task. Note that you might use -1 index meaning last element. I would do it following way

x=[1,2,3,4,5,8,11,12,13,18]
temp=[x[:1]]
for i in x[1:]:
 if temp[-1][-1]+1!=i: temp.append([])
 temp[-1].append(i)
print(temp)

Output:

[[1, 2, 3, 4, 5], [8], [11, 12, 13], [18]]

Explanation: I firstly load first element as one-element list, then for following elements, if there is difference other than 1 between current and last-seen element then I append new empty list to temp, then independently from full-filling or not condition I add current element to last sublist.

answered Jul 5, 2019 at 18:41

2 Comments

This answer does not answer why the code posted in the question does not work.
@Alex: added disclaimer to my answer
0
x=[1,2,3,4,5,8,11,12,13,18]
x.append(x[-1]-2)
temp=[]
firstnumber=0
for i in range(1, len(x)):
 current=x[i]
 previous=x[i-1]
 if ((current-previous)!=1):
 mm=(x[firstnumber:i])
 temp.append(mm)
 firstnumber=i
print(temp)
answered Jul 5, 2019 at 18:35

1 Comment

This answer is no different from the one I posted and offers no explanation.
-1

In the code, the variable firstnumber is, I believe, supposed to contain the index of the first element of any continuous segment of consecutive numbers.

However, when you do firstnumber=x[i] that purpose is lost. Instead you can do, firstnumber = i and then it will work.

Also, this implementation will not append the last consecutive segment. As a result, you will have to do that outside the loop.

answered Jul 5, 2019 at 18:33

2 Comments

It will not work simply making firstnumber = i there are several other problems.
i did but i'm getting [[1, 2, 3, 4], []] as the output now.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.