Changeset 996 in lmdz_wrf


Ignore:
Timestamp:
Aug 10, 2016, 1:09:23 PM (8 years ago)
Author:
lfita
Message:

Adding `dictVeysVals_stringList': Function to provide strings from a dictionary with keys which contain values of lists

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r995 r996  
    3636# dictionary_key: Function to provide the first key in a dictionay with a given value
    3737# dictionary_key_list: Function to provide the first key in a dictionay of lists with a given value
     38# dictVeysVals_stringList: Function to provide strings from a dictionary with keys which contain values of lists
    3839# files_folder_HMT: Function to retrieve a list of files from a folder [fold] and files with [head]*[middle]*[tail]
    3940# get_configuration: Function to get the configuration from an ASCII external file
     
    81188119
    81198120    for key in dictv.keys():
    8120         print '#  ' + key + ': ', dictv[key]
     8121        print '    # ' + key + ':', dictv[key]
    81218122
    81228123    return
     
    81288129      M= middle of the key (empty for nothing)
    81298130      T= tail of the key (empty for nothing)
    8130     dictrom = {'i':1, 'ii':2, 'iii':3, 'iv':4, 'v':5, 'vi':6, 'vii':2, 'viii':8, 'ix':9, 'x':10, 'xi':11 }
     8131    >>> dictrom = {'i':1, 'ii':2, 'iii':3, 'iv':4, 'v':5, 'vi':6, 'vii':2, 'viii':8, 'ix':9, 'x':10, 'xi':11 }
    81318132    >>> get_specdictionary_HTM(dictrom,H='i')
    81328133    {'ii': 2, 'ix': 9, 'iii': 3, 'i': 1, 'iv': 4}
     
    82078208    return newdictv
    82088209
    8209 dictrom = {'i':1, 'ii':2, 'iii':3, 'iv':4, 'v':5, 'vi':6, 'vii':2, 'viii':8, 'ix':9, 'x':10, 'xi':11 }
    8210 print get_specdictionary_HMT(dictrom,H='i')
    8211 print get_specdictionary_HMT(dictrom,M='i')
    8212 print get_specdictionary_HMT(dictrom,T='i')
    8213 print get_specdictionary_HMT(dictrom,H='v',M='i')
    8214 print get_specdictionary_HMT(dictrom,H='x',T='i')
    8215 print get_specdictionary_HMT(dictrom,M='i',T='x')
    8216 print get_specdictionary_HMT(dictrom,H='v',M='i',T='i')
     8210def dictVeysVals_stringList(dictv,cD=',',cK='@',cV='|'):
     8211    """ Function to provide strings from a dictionary with keys which contain values of lists
     8212      dictv= dictionary with the values
     8213      cD= character to use to separate keys
     8214      cK= character to use to separate keys from their values
     8215      cV= character to use to separate the values for each key
     8216    >>> dictvals = {'Spain': ['Madrid', 'Santander', 'Sevilla', 'Toledo'], 'France': ['Paris', 'Marseille', 'Tolouse']}
     8217    Spain@Madrid|Santander|Sevilla|Toledo,France@Paris|Marseille|Tolouse
     8218    """
     8219    fname = 'dictVeysVals_stringList'
     8220
     8221    ik = 1
     8222    for key in dictv.keys():
     8223        if ik == 1:
     8224            string = key + cK
     8225        else:
     8226            string = string + cD + key + cK
     8227
     8228        iv = 1
     8229        for val in dictv[key]:
     8230            if iv == 1:
     8231                string = string + val
     8232            else:
     8233                string = string + cV + val
     8234            iv = iv + 1
     8235
     8236        ik = ik + 1
     8237    return string
     8238
    82178239#quit()
    82188240
Note: See TracChangeset for help on using the changeset viewer.