6.2.4.2.22. invdict

Comienzo python section to interscript/core/mxTools.py[23 /26 ] Siguiente Previo Primero Último
   163: #line 1845 "mxTools.pak"
   164: def invdict(dictionary):
   165:   adict = {}
   166:   for key in dictionary.keys():
   167:     adict[dictionary[key]] = key
   168:   return adict
   169: 
   170: # EXTENSION: proper inverse of dictionary,
   171: # returns dictionary of lists
   172: def proper_invdict(dictionary):
   173:   adict = {}
   174:   for key in dictionary.keys():
   175:     value = dictionary[key]
   176:     if not adict.has_key(value):
   177:       adict[value]=[]
   178:     adict[value].append(key)
   179:   return adict
   180: 
End python section to interscript/core/mxTools.py[23]
Comienzo C section to interscript/core/mxTools.c[24 /30 ] Siguiente Previo Primero Último
  1585: #line 1863 "mxTools.pak"
  1586: 
  1587: Py_C_Function( mxTools_invdict,
  1588:                "invdict(d)\n\n"
  1589:                "Creates a dictionary with inverse mappings from the\n"
  1590:                "given dictionary d.")
  1591: {
  1592:     int pos;
  1593:     PyObject *key,*value;
  1594:     PyObject *d,*inv = 0;
  1595: 
  1596:     Py_GetArgObject(d);
  1597:     Py_Assert(PyDict_Check(d),
  1598:               PyExc_TypeError,
  1599:               "argument must be a dictionary");
  1600: 
  1601:     inv = PyDict_New();
  1602:     if (!inv)
  1603:         goto onError;
  1604: 
  1605:     pos = 0;
  1606:     while (PyDict_Next(d, &pos, &key, &value)) {
  1607:         if (PyDict_SetItem(inv,value,key))
  1608:             goto onError;
  1609:     }
  1610: 
  1611:     return inv;
  1612:  onError:
  1613:     Py_XDECREF(inv);
  1614:     return NULL;
  1615: }
  1616: 
End C section to interscript/core/mxTools.c[24]