1

I have a list of integers as following

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, -64, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1]

How would I go about converting them into:

[(1, 10), (7, 12), (-64, 1), (9, 9), (1, 3)]

Or, in words, a tuple of (the integer, the amount of integers in a row)

I have tried the following

diff = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, -64, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1]

found = 0
new = []
for i,j in zip(diff, diff[1:]):
    if i == j:
        found += 1
    else:
        new.append((i, found))
        found = 0

But it doesn't look great and doesn't work either, returns [(1, 17), (7, 11), (-64, 0)]

Dimsey
  • 43
  • 5

0 Answers0