Changeset 1034 in lmdz_wrf


Ignore:
Timestamp:
Aug 21, 2016, 12:59:49 AM (9 years ago)
Author:
lfita
Message:

Adding `list_combos': Function to construct a new list with all possible N-combinations of the list-values
Effectively remove of all `statistics' from variables' names in 'variables_values'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1030 r1034  
    3838# contflow: Function to bring back the increment in j,i grid points according to a trip: (inflow directions)
    3939# dictionary_key: Function to provide the first key in a dictionay with a given value
    40 # dictionary_key_list: Function to provide the first key in a dictionay of lists with a given value
     40# dictionary_key_list: Function to provide the first key in a dictionary of lists with a given value
    4141# dictKeysVals_stringList: Function to provide strings from a dictionary with keys which contain values of lists
    4242# dictvar_listS: Function to provide a Dc string separated list of DVs separated [key] [value] couples following a list of keys
     
    4747# incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point
    4848# index_mat_way: Function to look for a value within a matrix following a direction
     49# list_combos: Function to construct a new list with all possible N-combinations of the list-values
    4950# num_chainSnum: Function to pass a value to a `ChainStrNum' number
    5051# num_split: Function to split a string at each numeric value keeping the number as the last character of each cut
     
    250251
    251252    return newstring
     253
     254def list_combos(listv,Ncombos):
     255    """ Function to construct a new list with all possible N-combinations of the list-values
     256      listv= list of values
     257      Ncombos= number of combinations
     258      >>> list_combos(['a', 'b', 'c', 'd'],1)
     259      ['a', 'b', 'c', 'd', 'aa', 'ab', 'ac', 'ad', 'ba', 'bb', 'bc', 'bd', 'ca', 'cb', 'cc', 'cd', 'da', 'db', 'dc', 'dd']
     260    """
     261    fname = 'list_combos'
     262
     263    newlist = list(listv)
     264    Nlvs = len(listv)
     265    for ic in range(Ncombos):
     266        print fname + ' Lluis: ic:', ic, 'Nlvs:', Nlvs
     267        prevlist = list(newlist)
     268        for lv in prevlist:
     269            for ilv in listv:
     270                newlist.append(lv + ilv)
     271
     272    return newlist
    252273
    253274def period_information(idate, edate, totunits):
     
    544565                print '    '+ fname + ": varibale '" + varName + "' with a " +       \
    545566                  "statistical surname: '",st,"' !!"
    546                 Lst = len(st)
    547                 LvarName = len(varName)
    548                 varn = varName[0:LvarName - Lst]
     567                varName = varName.replace(st,'')
    549568                ifst = True
    550                 break
    551     if not ifst:
    552         varn = varName
     569    varn = varName
    553570
    554571    ncf = open(infile, 'r')
Note: See TracChangeset for help on using the changeset viewer.