Python - in place list partition -


is there way partition list predicate in place using standard python 2.7( c++ stl std::partition style) ? group_by itertools without crating additional arrays ? need recursively partition array 2 groups, based on variadic conditions , limited amount of ram.

what looking function like:

partitioncppstyle(data, startindex, endindex, condition)

which result in data[startindex:endindex] list having elements satisfying condition @ beginning , returning index of first element doesn't fullfil condition. no copies, little memory used possible.

i've ended writing own implementation:

def partitioninplace(data, startindex, endindex, predicate):     swapindex = endindex     index = startindex     while(index < swapindex):         if not predicate(data[index]):             temp = data[swapindex]             data[swapindex] = data[index]             data[index] = temp             swapindex = swapindex-1         else:             index = index+1     return index 

is there more efficient way it?

it's easy quicksort,you can own

def partitioncppstyle(data, a, b, condition):     if a>= b:return     left =     right = b     while left <= right:         while left <= right , condition(data[left]):             left += 1         while left <= right , not condition(data[right]):             right -= 1         if left <= right:             data[left], data[right] = data[right], data[left]             left, right = left + 1, right - 1  def less7(num):     return num < 7 if __name__ == '__main__':     data = [2,34,6,1,3232,32]     partitioncppstyle(data, 0, 5, less7)     print(data) 

it's c.but it's easy , good.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -