6.2.4.2.12. lists

Comienzo python section to interscript/core/mxTools.py[13 /26 ] Siguiente Previo Primero Último
    92: #line 1227 "mxTools.pak"
    93: # this function should also be called transpose :-)
    94: def lists(*sequence):
    95:   if len(sequence) == 1: sequence = sequence[0]
    96:   m = len(sequence[0])
    97:   alist = apply(map,(None,)+tuple(sequence))[:m]
    98:   result = tuple(map(list, alist))
    99:   #for i in range(len(alist)):
   100:   #  alist[i]=list(alist[i])
   101:   #result = tuple(alist)
   102:   print 'lists'+`sequence`+'='+`result`
   103:   return result
   104: 
End python section to interscript/core/mxTools.py[13]
Comienzo C section to interscript/core/mxTools.c[14 /30 ] Siguiente Previo Primero Último
  1048: #line 1240 "mxTools.pak"
  1049: 
  1050: Py_C_Function( mxTools_lists,
  1051:                "lists(sequence)\n\n"
  1052:                "Same as tuples(), except that a tuple of lists is created."
  1053:                )
  1054: {
  1055:     int n,m;
  1056:     register int i;
  1057:     register int j;
  1058:     PyObject *t = 0;
  1059:     PyObject *arg,*w;
  1060: 
  1061:     Py_GetArgObject(arg);
  1062: 
  1063:     /* Get list size (n) */
  1064:     n = PySequence_Length(arg);
  1065:     Py_Assert(n > 0,
  1066:               PyExc_TypeError,
  1067:               "sequence must have at least one element");
  1068: 
  1069:     /* Get tuple size (m) */
  1070:     w = PySequence_GetItem(arg,0);
  1071:     if (!w)
  1072:         goto onError;
  1073:     m = PySequence_Length(w);
  1074:     Py_DECREF(w);
  1075:     Py_Assert(m >= 0,
  1076:               PyExc_TypeError,
  1077:               "sequence elements must be sequences");
  1078: 
  1079:     /* XXX Could speed this up by rearranging and joining the loops */
  1080: 
  1081:     /* Create tuple of lists */
  1082:     t = PyTuple_New(m);
  1083:     if (!t)
  1084:         goto onError;
  1085:     for (j = 0; j < m; j++) {
  1086:         PyObject *v;
  1087: 
  1088:         v = PyList_New(n);
  1089:         if (!v)
  1090:             goto onError;
  1091:         PyTuple_SET_ITEM(t,j,v);
  1092:     }
  1093: 
  1094:     /* Fill them in */
  1095:     for (i = 0; i < n; i++) {
  1096:         PyObject *u;
  1097: 
  1098:         u = PySequence_GetItem(arg,i);
  1099:         if (!u)
  1100:             goto onError;
  1101: 
  1102:         for (j = 0; j < m; j++) {
  1103:             PyObject *v;
  1104: 
  1105:             v = PySequence_GetItem(u,j);
  1106:             if (!v) {
  1107:                 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
  1108:                     PyErr_Clear();
  1109:                     /* Fill up the rest with None */
  1110:                     for (; j < n; j++) {
  1111:                         Py_INCREF(Py_None);
  1112:                         PyList_SET_ITEM(PyTuple_GET_ITEM(t,j),i,Py_None);
  1113:                     }
  1114:                     break;
  1115:                 }
  1116:                 else {
  1117:                     Py_DECREF(u);
  1118:                     goto onError;
  1119:                 }
  1120:             }
  1121:             PyList_SET_ITEM(PyTuple_GET_ITEM(t,j),i,v);
  1122:         }
  1123:         Py_DECREF(u);
  1124:     }
  1125:     return t;
  1126: 
  1127:  onError:
  1128:     Py_XDECREF(t);
  1129:     return NULL;
  1130: }
  1131: 
End C section to interscript/core/mxTools.c[14]