Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding longest run in a list

Tags:

python

list

Given a list of data, I'm trying to create a new list in which the value at position i is the length of the longest run starting from position i in the original list. For instance, given

x_list = [1, 1, 2, 3, 3, 3]

Should return:

run_list = [2, 1, 1, 3, 2, 1]

My solution:

freq_list = []
current = x_list[0]
count = 0
for num in x_list:
    if num == current:
        count += 1
    else:
        freq_list.append((current,count))
        current = num
        count = 1
freq_list.append((current,count))

run_list = []
for i in freq_list:
    z = i[1]
    while z > 0:
        run_list.append(z)
        z -= 1 

Firstly I create a list freq_list of tuples, where every tuple's first element is the element from x_list, and where the second element is the number of the total run.

In this case:

freq_list = [(1, 2), (2, 1), (3, 3)]

Having this, I create a new list and append appropriate values.

However, I was wondering if there is a shorter way/another way to do this?

like image 602
meriam Avatar asked Apr 20 '18 21:04

meriam


4 Answers

Here's a simple solution that iterates over the list backwards and increments a counter each time a number is repeated:

last_num = None
result = []
for num in reversed(x_list):
    if num != last_num:
        # if the number changed, reset the counter to 1
        counter = 1
        last_num = num
    else:
        # if the number is the same, increment the counter
        counter += 1

    result.append(counter)

# reverse the result
result = list(reversed(result))

Result:

[2, 1, 1, 3, 2, 1]
like image 111
Aran-Fey Avatar answered Oct 21 '22 10:10

Aran-Fey


This is possible using itertools:

from itertools import groupby, chain

x_list = [1, 1, 2, 3, 3, 3]

gen = (range(len(list(j)), 0, -1) for _, j in groupby(x_list))
res = list(chain.from_iterable(gen))

Result

[2, 1, 1, 3, 2, 1]

Explanation

  • First use itertools.groupby to group identical items in your list.
  • For each item in your groupby, create a range object which counts backwards from the length of the number of consecutive items to 1.
  • Turn this all into a generator to avoid building a list of lists.
  • Use itertools.chain to chain the ranges from the generator.

Performance note

Performance will be inferior to @Aran-Fey's solution. Although itertools.groupby is O(n), it makes heavy use of expensive __next__ calls. These do not scale as well as iteration in simple for loops. See itertools docs for groupby pseudo-code.

If performance is your main concern, stick with the for loop.

like image 24
jpp Avatar answered Oct 21 '22 11:10

jpp


You are performing a reverse cumulative count on contiguous groups. We can create a Numpy cumulative count function with

import numpy as np

def cumcount(a):
    a = np.asarray(a)
    b = np.append(False, a[:-1] != a[1:])
    c = b.cumsum()
    r = np.arange(len(a))
    return r - np.append(0, np.flatnonzero(b))[c] + 1

and then generate our result with

a = np.array(x_list)

cumcount(a[::-1])[::-1]

array([2, 1, 1, 3, 2, 1])
like image 6
piRSquared Avatar answered Oct 21 '22 09:10

piRSquared


I would use a generator for this kind of task because it avoids building the resulting list incrementally and can be used lazily if one wanted:

def gen(iterable):  # you have to think about a better name :-)
    iterable = iter(iterable)
    # Get the first element, in case that fails
    # we can stop right now.
    try:
        last_seen = next(iterable)
    except StopIteration:
        return
    count = 1

    # Go through the remaining items
    for item in iterable:
        if item == last_seen:
            count += 1
        else:
            # The consecutive run finished, return the
            # desired values for the run and then reset
            # counter and the new item for the next run.
            yield from range(count, 0, -1)
            count = 1
            last_seen = item
    # Return the result for the last run
    yield from range(count, 0, -1)

This will also work if the input cannot be reversed (certain generators/iterators cannot be reversed):

>>> x_list = (i for i in range(10))  # it's a generator despite the variable name :-)
>>> ... arans solution ...
TypeError: 'generator' object is not reversible

>>> list(gen((i for i in range(10))))
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

And it works for your input:

>>> x_list = [1, 1, 2, 3, 3, 3]
>>> list(gen(x_list))
[2, 1, 1, 3, 2, 1]

This can actually be made simpler by using itertools.groupby:

import itertools

def gen(iterable):
    for _, group in itertools.groupby(iterable):
        length = sum(1 for _ in group)  # or len(list(group))
        yield from range(length, 0, -1)

>>> x_list = [1, 1, 2, 3, 3, 3]
>>> list(gen(x_list))
[2, 1, 1, 3, 2, 1]

I also did some benchmarks and according to these Aran-Feys solution is the fastest except for long lists where piRSquareds solution wins:

enter image description here

This was my benchmarking setup if you want to confirm the results:

from itertools import groupby, chain
import numpy as np

def gen1(iterable):
    iterable = iter(iterable)
    try:
        last_seen = next(iterable)
    except StopIteration:
        return
    count = 1
    for item in iterable:
        if item == last_seen:
            count += 1
        else:
            yield from range(count, 0, -1)
            count = 1
            last_seen = item
    yield from range(count, 0, -1)

def gen2(iterable):
    for _, group in groupby(iterable):
        length = sum(1 for _ in group)
        yield from range(length, 0, -1)

def mseifert1(iterable):
    return list(gen1(iterable))

def mseifert2(iterable):
    return list(gen2(iterable))

def aran(x_list):
    last_num = None
    result = []
    for num in reversed(x_list):
        if num != last_num:
            counter = 1
            last_num = num
        else:
            counter += 1
        result.append(counter)
    return list(reversed(result))

def jpp(x_list):
    gen = (range(len(list(j)), 0, -1) for _, j in groupby(x_list))
    res = list(chain.from_iterable(gen))
    return res

def cumcount(a):
    a = np.asarray(a)
    b = np.append(False, a[:-1] != a[1:])
    c = b.cumsum()
    r = np.arange(len(a))
    return r - np.append(0, np.flatnonzero(b))[c] + 1

def pirsquared(x_list):
    a = np.array(x_list)
    return cumcount(a[::-1])[::-1]

from simple_benchmark import benchmark
import random

funcs = [mseifert1, mseifert2, aran, jpp, pirsquared]
args = {2**i: [random.randint(0, 5) for _ in range(2**i)] for i in range(1, 20)}

bench = benchmark(funcs, args, "list size")

%matplotlib notebook
bench.plot()

Python 3.6.5, NumPy 1.14

like image 6
MSeifert Avatar answered Oct 21 '22 09:10

MSeifert