From df29d0378bc8a690ccb308b504c89473cd87be29 Mon Sep 17 00:00:00 2001 From: linarphy <linarphy@linarphy.net> Date: Mon, 19 Jun 2023 13:31:50 +0200 Subject: [PATCH] Improve same_value efficiency --- utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/utils.py b/utils.py index 39e4511..550d53a 100644 --- a/utils.py +++ b/utils.py @@ -210,10 +210,8 @@ def same_value( list_ ): raise ValueError( 'list_ must be a list, ' + type( list_ ) + ' given' ) if len( list_ ) == 0: return list_ - index = last_same_value( list_ ) - if index == len( list_ ) - 1: - return [ list_ ] - return same_value( list_[ : index + 1 ] ) + same_value( list_[ index + 1 : ] ) + counter = np.arange( 1 , len( list_ ) ) + return np.split( list_ , counter[ list_[ 1 : ] != list_[ : - 1 ] ] ) def last_same_value( list_ ): """ return the last index of the first same value list