Segmenting lists

list1 = [ 2, 3, 4, 9, 10]

#target output = [2, 3], [9, 2]

Newbie here. I’d like to segment the numbers that are adjacent to each other in the form [a, b] where a is the start number and b is the length of that segment. Not sure if I’m just stuffing myself over here or what, any help is appreciated :))

Note: we try not to provide outright solutions; solving the problem
yoursef is the useful outcome. That said, happy to describe approaches
or explain problems.

Are you saying that the list1 has 2 runs of consecutive increasing
numbers, one starting at the value 2 with 3 members, and then on
starting at the value 9 with 2 members. So:

[2, 3, 4, 9, 10, 13, 17]

should produce:

[2, 3], [9, 2], [13, 1], [17, 1]

If so, I would (a) prepare a representation of the “current” run then
(b) iterate over the list looking at the run, bu comparing the current
value with the previous value. If it is the expected n+1, bump the
representation count by 1, otherwise (a) save the current representation
eg [2,3] and commence a new representation i.e. [9,1]. Proceed.

After the loop, if you’ve got a representation, save it too (because
you’ve run off the end of the input list, and not yet saved that run).

If that seems clear, have a run at it and come back if you have probems.
If not, ask questions.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Start by writing a function that takes two arguments, a list and a
position, and iterates over that list starting at that position so long
as the items are consecutive. It should return the count of consecutive
items starting from that position. Here is some test data you can run to
ensure your function gives the correct results:

def count_consecutive(alist, index):
    # write some code here

# Test data
alist = [4, 6, 7, 8, 12, 13, 14, 15, 19, 20]

assert count_consecutive(alist, 0) == 1
assert count_consecutive(alist, 1) == 3
assert count_consecutive(alist, 2) == 2
assert count_consecutive(alist, 3) == 1
assert count_consecutive(alist, 4) == 4
assert count_consecutive(alist, 5) == 3
assert count_consecutive(alist, 6) == 2
assert count_consecutive(alist, 7) == 1
assert count_consecutive(alist, 8) == 2
assert count_consecutive(alist, 9) == 1

If you have trouble writing count_consecutive, start by doing the
process using pencil and paper. How would you count the consecutive
items? Now imagine you have to explain it to a simpleton, step by step,
so you have to explain every baby step needed.

Python is that simpleton. Start by a description of the steps in
ordinary English (or whatever your native language is), and then change
that to Python. We can help with that if you get stuck.

Once you have that count_consecutive function, you can use that to walk
through the list.

def get_consecutive_counts(alist):
    # Start at the beginning.
    index = 0
    # Get the number at that index.
    num = alist[index]
    # Count the consecutive items.
    count = count_consecutive(alist, index)
    # Print the desired values.
    print(num, count)
    # Jump to the next non-consecutive item.
    index = index + count
    # and so on...

Once you have that working correctly (hint: you will need a while loop)
change the code to accumulate the (num, count) pairs into a list instead
of printing them.