Welcome, guest | Sign In | My Account | Store | Cart

A collection of some dictionary tools

Python, 291 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#========================================================================

def sumDict(d): 
    return reduce(lambda x,y:x+y, d.values())

#========================================================================

def avgDict(d): 
    return reduce(lambda x,y:x+y, d.values()) / (len(d)*1.0)

#========================================================================

def mkDict(keyList, valueList, cond=None):
    '''
    Make a new dict out of two lists. The first list provides keys,
    the 2nd provides values. The cond is a function taking 2 arguments:
    key and value. Example: lambda k,v:v%2==0
    A valid item means its cond(key, value) is true. Only valid items
    are included in the returned dict. 

    >>> a
    [0, 1, 2, 3, 4]
    
    >>> b= [chr(x) for x in range(65,70)]
    >>> b
    ['A', 'B', 'C', 'D', 'E']
    
    >>> mkDict(b,a)
    {'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3}
    
    >>> mkDict(b,a,lambda x,y:y%2==0)
    {'A': 0, 'C': 2, 'E': 4}

    Note:
    mkDict(a,b) (without the conditional check) is equavilant to
    dict(zip(a,b)), which is much faster. If you know there's no
    conditional check, better use dict(zip(a,b)) instead.
    '''

    if cond==None:
        return dict(zip(keyList, valueList))        
    else:
        return dict( [(k,v) for k,v in zip(keyList, valueList) if cond(k,v)] )

#========================================================================

def trimDict(aDict, cond=(lambda k,v:1)):
    ''' Return a new dict in which its items whose cond(k,v) == true
    are removed (discarded) from aDict.
    The cond is a function taking 2 arguments: key and value
    
    >>> g
    {'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3}
    >>> trimDict(g, lambda x,y:y%2!=0)
    {'A': 0, 'C': 2, 'E': 4}
    
    '''    
    tmp={}
    [tmp.setdefault(k,v) for k,v in aDict.items() if not cond(k,v)]
    return tmp.copy()

#========================================================================

def reDict(aDict, func, cond=(lambda k,v:1), delUnchanged=0):
    '''
    Given aDict, return a new dict with valid items (= items whose
    cond(key, value) is true) been modified by the function func.
    if delUnchanged, then those invalid items are excluded from
    the returned dict.
    
    >>> d
    {'B': 1, 'D': 3}
    >>> reDict(d, lambda x:x**2)
    {'B': 1, 'D': 9}
    >>> g
    {'A': 0, 'C': 2, 'B': 1, 'E': 4, 'D': 3}
    >>> reDict(g, lambda x:-x, lambda k,v: v%2==0)
    {'A': 0, 'C': -2, 'B': 1, 'E': -4, 'D': 3}
    >>> reDict(g, lambda x:-x, lambda k,v: v%2==0, delUnchanged=1)
    {'A': 0, 'C': -2, 'E': -4}    

    '''
    tmp={}
    if delUnchanged:
       [tmp.setdefault(k,func(v)) for k,v in aDict.items() if cond(k,v)]
    else:
       [tmp.setdefault(k,(cond(k,v) and func(v) or v)) for k,v in aDict.items()]
    return tmp.copy()

#========================================================================

def normDict(d, normalizeTo=1):
  ''' Normalize the values of d by setting the largest value in d.values
      to normalizeTo. The return dict will have it's values spreading
      between 0~normalizeTo'''
  ks = d.keys()
  vs = d.values()
  vMax = max(vs)
  vs= [ x/(vMax*1.0)*normalizeTo for x in vs]
  return dict(zip(ks, vs))

#========================================================================

def normDictSumTo(d, sumTo=1):
  ''' Normalize the values of d to sumTo. The returned dict will
      have value sum = sumTo
  '''
  ks = d.keys()
  vs = d.values()  
  sum = reduce(lambda x,y:x+y, vs)
  vs= [ x/(sum*1.0)*sumTo for x in vs]
  return dict(zip(ks, vs))

#========================================================================

def accumDict(d, normalizeTo=None):
    ''' if d.values=[1,2,3,4,5]: 
        accumDict(d).values ==>   [1, 3, 6, 10, 15]
        if d.values=[0.25, 0.25, 0.25, 0.25]
        accumDict(d).values ==>   [0.25, 0.50, 0.75, 1.00]

        Required: odict()
        Ordered Dictionary Class
        Dave Benjamin 
        http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/161403
    '''
    ks = d.keys()
    vs = d.values()
    if normalizeTo: vs = normListSumTo(vs, sumTo=normalizeTo)

    r = range(1, len(vs))
    newList=[vs[0]]
    for i in r:
        newList.append( newList[-1]+ vs[i] )
    
    odic = odict()
    for i in range(len(newList)):
        odic[ks[i]] =newList[i]
    return odic 

#========================================================================

def fallInDict(val, aDict, baseValue=0):
    '''
    oo:
    {
     'a':3,
     'b':5,
     'c':2
    }
    accumDict(oo):
    {
     'a':3,
     'b':8,
     'c':10
    }
    fallInDict(0, oo)=0
    fallInDict(1, oo)=0
    fallInDict(2, oo)=0
    fallInDict(3, oo)=0
    fallInDict(4, oo)=1
    fallInDict(5, oo)=1
    fallInDict(6, oo)=1
    fallInDict(7, oo)=1
    fallInDict(8, oo)=1
    fallInDict(9, oo)=2
    fallInDict(10, oo)=2

    When val out of range:
    
    fallInDict(-1, oo)= -1
    fallInDict(11, oo)= -2
    fallInDict(12, oo)= -2
    '''

    if val<baseValue: return -1
    acd = accumDict(aDict)
    if val>acd.values()[-1]: return -2

    ra = range(len(acd))

    for i in ra:
        vInDict = acd.values()[i]
        if val <= vInDict: break

    return aDict.keys()[i]  

#========================================================================

def randomPickDict(d):
    ''' given a diictionary d, with all values are numbers,
        randomly pick an item and return it's key according
        to the percentage of its values'''
    ks = d.keys()
    vs = accumList(d.values(),1)
    return ks[ findIndex( vs, random.random()) ]

#========================================================================

def sumInDict(L):
  ''' Given a list L:
     [ {'S': [s11,s12,s13], 'V':[v11,v12,v13], ...},
       {'S': [s21,s22,s23], 'V':[v21,v22,v23], ...},
       {'S': [s31,s32,s33], 'V':[v31,v32,v33], ...},
       ... ]
       
     Return a dict:
     {'S': [s1,s2,s3], 'V':[v1,v2,v3], ...}

     where s1 = s11 + s21 + s31 + ...
           s2 = s12 + s22 + s32 + ...
  '''  
  keys = L[0].keys()
  new={}
  for k in keys:
      new[k] = []   # Init 'S':[]
      for x in L:   # x = {'S': [s11,s12,s13], 'V':[v11,v12,v13], ...}
          y = x[k]  # y = [s11,s12,s13]
          new[k].append(y) 

      ''' new[k] = [ [s11,s12,s13],[s21,s22,s23],[s31,s32,s33], ...]
          Then use sumInList=> {'S': [s1,s2,s3],'V': [v1,v2,v3],... }'''
      new[k] = sumInList(new[k])
  return new   

#========================================================================

def avgInDict(L):
  ''' Given a list L:
     [ {'S': [s11,s12,s13], 'V':[v11,v12,v13], ...},
       {'S': [s21,s22,s23], 'V':[v21,v22,v23], ...},
       {'S': [s31,s32,s33], 'V':[v31,v32,v33], ...},
       ... ]
       
     Return a dict:
     {'S': [s1,s2,s3], 'V':[v1,v2,v3], ...}

     where s1 = avg of (s11 + s21 + s31 + ...)
           s2 = avg of (s12 + s22 + s32 + ...)
  '''  
  ''' First turn L into:
      {'S': [ [s11,s12,s13],[s21,s22,s23],[s31,s32,s33], ...],
       'V': [ [v11,v12,v13],[v21,v22,v23],[v31,v32,v33], ...],
       ... }  '''

  keys = L[0].keys()
  new={}
  for k in keys:
      new[k] = []   # Init 'S':[]
      for x in L:   # x = {'S': [s11,s12,s13], 'V':[v11,v12,v13], ...}
          y = x[k]  # y = [s11,s12,s13]
          new[k].append(y) 

      ''' new[k] = [ [s11,s12,s13],[s21,s22,s23],[s31,s32,s33], ...]
          Then use avgInList=> {'S': [s1,s2,s3],'V': [v1,v2,v3],... }'''
      new[k] = avgInList(new[k])
  return new    

#========================================================================

def sumInDict2(D):
  ''' Given a dict:
        {'A':{'S': [s11,s12,s13], 'V':[v11,v12,v13], ...},
         'B':{'S': [s21,s22,s23], 'V':[v21,v22,v23], ...},
         'C':{'S': [s31,s32,s33], 'V':[v31,v32,v33], ...}, ...
        } 
     Return a dict:
     {'S': [s1,s2,s3], 'V':[v1,v2,v3], ...}

     where s1 = s11 + s21 + s31 + ...
           s2 = s12 + s22 + s32 + ...
  '''
  return sumInDict(D.values())

#========================================================================

def avgInDict2(D):
  ''' Given a dict:
        {'A':{'S': [s11,s12,s13], 'V':[v11,v12,v13], ...},
         'B':{'S': [s21,s22,s23], 'V':[v21,v22,v23], ...},
         'C':{'S': [s31,s32,s33], 'V':[v31,v32,v33], ...}, ...
        } 
     Return a dict:
     {'S': [s1,s2,s3], 'V':[v1,v2,v3], ...}

     where s1 = avg of (s11 + s21 + s31 + ...)
           s2 = avg of (s12 + s22 + s32 + ...)
  '''
  return avgInDict(D.values())

#========================================================================

Example: *****[ randomPickDict ]***** p = {'a':1, 'c':6, 'b':3 } s = {'a':0, 'b':0, 'c':0 }

Pick 1000 times to see if it works:

for i in range(1000): k =randomPickDict(p) s[k]+=1 s: { 'a':89, 'c':623, 'b':288 }


Reference:

--- List Tools (Runsun Pan): http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278258

--- odict()(Dave Benjamin): http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/161403

2 comments

andreas.kuntzagk 16 years, 3 months ago  # | flag

findIndex. where does the findIndex in randomPickDict(d) comes from?

andreas.kuntzagk 16 years, 3 months ago  # | flag

Sorry. overlooked the links at the bottom.

Created by Runsun Pan on Wed, 14 Apr 2004 (PSF)
Python recipes (4591)
Runsun Pan's recipes (8)

Required Modules

  • (none specified)

Other Information and Tasks