python - update all minimums in list to the next minimum -


how increment numbers in python list such current minimums in list updated. example:

a = [15,15,14,12,10,10,10] 

i have number x=12 need optimally allocate array such maximal minimum, ie, first give 2,2,2 each 10 2,2,2 3 12s. , final list looks this:

a = [15,15,14,14,14,14,12] 

to update minimums in list next minimum can use following approach.

first determine smallest value using python's min function. calculate second smallest iterating on each of values. possible use list comprehension update value less second smallest value second smallest value:

import sys  = [15,15,14,12,10,10,10] smallest = min(a) second_smallest = sys.maxint        # largest possible allowed integer  x in a:     if smallest < x < second_smallest:         second_smallest = x  a[:] = [second_smallest if x < second_smallest else x x in a]  print 

this displays following:

[15, 15, 14, 12, 12, 12, 12] 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -