27 ################################################# 28 ####### Written by: Gary Strangman ########### 29 ####### Last modified: Dec 18, 2007 ########### 30 ################################################# 32 This module provides some useful list and array manipulation routines 33 modeled after those found in the |Stat package by Gary Perlman, plus a 34 number of other useful list/file manipulation functions. The list-based 38 simpleabut (source, addon) 39 colex (listoflists,cnums) 40 collapse (listoflists,keepcols,collapsecols,fcn1=None,fcn2=None,cfcn=None) 41 dm (listoflists,criterion) 43 linexand (listoflists,columnlist,valuelist) 44 linexor (listoflists,columnlist,valuelist) 45 linedelimited (inlist,delimiter) 46 lineincols (inlist,colsize) 47 lineincustcols (inlist,colsizes) 52 printincols (listoflists,colsize) 55 replace (lst,oldval,newval) 56 recode (inlist,listmap,cols='all') 57 remap (listoflists,criterion) 58 roundlist (inlist,num_digits_to_round_floats_to) 59 sortby(listoflists,sortcols) 62 writedelimited (listoflists, delimiter, file, writetype='w') 64 Some of these functions have alternate versions which are defined only if 65 Numeric (NumPy) can be imported. These functions are generally named as 66 above, with an 'a' prefix. 69 acolex (a,indices,axis=1) 70 acollapse (a,keepcols,collapsecols,sterr=0,ns=0) 72 alinexand (a,columnlist,valuelist) 73 alinexor (a,columnlist,valuelist) 74 areplace (a,oldval,newval) 75 arecode (a,listmap,col='all') 76 arowcompare (row1, row2) 82 Currently, the code is all but completely un-optimized. In many cases, the 83 array versions of functions amount simply to aliases to built-in array 84 functions/methods. Their inclusion here is for function name consistency. 122 Like the |Stat abut command. It concatenates two lists side-by-side 123 and returns the result. '2D' lists are also accomodated for either argument 124 (source or addon). CAUTION: If one list is shorter, it will be repeated 125 until it is as long as the longest list. If this behavior is not desired, 126 use pstat.simpleabut(). 128 Usage: abut(source, args) where args=any # of lists 129 Returns: a list of lists as long as the LONGEST list past, source on the 130 'left', lists in <args> attached consecutively on the 'right' 133 if type(source)
not in [ListType,TupleType]:
136 if type(addon)
not in [ListType,TupleType]:
138 if len(addon) < len(source):
139 if len(source) % len(addon) == 0:
140 repeats = len(source)/len(addon)
141 origadd = copy.deepcopy(addon)
142 for i
in range(repeats-1):
143 addon = addon + origadd
145 repeats = len(source)/len(addon)+1
146 origadd = copy.deepcopy(addon)
147 for i
in range(repeats-1):
148 addon = addon + origadd
149 addon = addon[0:len(source)]
150 elif len(source) < len(addon):
151 if len(addon) % len(source) == 0:
152 repeats = len(addon)/len(source)
153 origsour = copy.deepcopy(source)
154 for i
in range(repeats-1):
155 source = source + origsour
157 repeats = len(addon)/len(source)+1
158 origsour = copy.deepcopy(source)
159 for i
in range(repeats-1):
160 source = source + origsour
161 source = source[0:len(addon)]
169 Concatenates two lists as columns and returns the result. '2D' lists 170 are also accomodated for either argument (source or addon). This DOES NOT 171 repeat either list to make the 2 lists of equal length. Beware of list pairs 172 with different lengths ... the resulting list will be the length of the 175 Usage: simpleabut(source,addon) where source, addon=list (or list-of-lists) 176 Returns: a list of lists as long as source, with source on the 'left' and 179 if type(source)
not in [ListType,TupleType]:
181 if type(addon)
not in [ListType,TupleType]:
183 minlen = min(len(source),len(addon))
184 list = copy.deepcopy(source)
185 if type(source[0])
not in [ListType,TupleType]:
186 if type(addon[0])
not in [ListType,TupleType]:
187 for i
in range(minlen):
188 list[i] = [source[i]] + [addon[i]]
190 for i
in range(minlen):
191 list[i] = [source[i]] + addon[i]
193 if type(addon[0])
not in [ListType,TupleType]:
194 for i
in range(minlen):
195 list[i] = source[i] + [addon[i]]
197 for i
in range(minlen):
198 list[i] = source[i] + addon[i]
205 Extracts from listoflists the columns specified in the list 'cnums' 206 (cnums can be an integer, a sequence of integers, or a string-expression that 207 corresponds to a slice operation on the variable x ... e.g., 'x[3:]' will colex 208 columns 3 onward from the listoflists). 210 Usage: colex (listoflists,cnums) 211 Returns: a list-of-lists corresponding to the columns from listoflists 212 specified by cnums, in the order the column numbers appear in cnums 216 if type(cnums)
in [ListType,TupleType]:
218 column = map(
lambda x: x[index], listoflists)
219 for col
in cnums[1:]:
221 column =
abut(column,map(
lambda x: x[index], listoflists))
222 elif type(cnums) == StringType:
223 evalstring =
'map(lambda x: x'+cnums+
', listoflists)' 224 column = eval(evalstring)
227 column = map(
lambda x: x[index], listoflists)
231 def collapse (listoflists,keepcols,collapsecols,fcn1=None,fcn2=None,cfcn=None):
233 Averages data in collapsecol, keeping all unique items in keepcols 234 (using unique, which keeps unique LISTS of column numbers), retaining the 235 unique sets of values in keepcols, the mean for each. Setting fcn1 236 and/or fcn2 to point to a function rather than None (e.g., stats.sterr, len) 237 will append those results (e.g., the sterr, N) after each calculated mean. 238 cfcn is the collapse function to apply (defaults to mean, defined here in the 239 pstat module to avoid circular imports with stats.py, but harmonicmean or 240 others could be passed). 242 Usage: collapse (listoflists,keepcols,collapsecols,fcn1=None,fcn2=None,cfcn=None) 243 Returns: a list of lists with all unique permutations of entries appearing in 244 columns ("conditions") specified by keepcols, abutted with the result of 245 cfcn (if cfcn=None, defaults to the mean) of each column specified by 248 def collmean (inlist):
252 return s/float(len(inlist))
254 if type(keepcols)
not in [ListType,TupleType]:
255 keepcols = [keepcols]
256 if type(collapsecols)
not in [ListType,TupleType]:
257 collapsecols = [collapsecols]
261 means = [0]*len(collapsecols)
262 for i
in range(len(collapsecols)):
263 avgcol =
colex(listoflists,collapsecols[i])
264 means[i] = cfcn(avgcol)
270 means[i] = [means[i], test]
277 means[i] = means[i] + [len(avgcol)]
279 means[i] = [means[i],len(avgcol)]
282 values =
colex(listoflists,keepcols)
286 if type(keepcols)
not in [ListType,TupleType]: keepcols = [keepcols]
288 if type(item)
not in [ListType,TupleType]: item =[item]
289 tmprows =
linexand(listoflists,keepcols,item)
290 for col
in collapsecols:
291 avgcol =
colex(tmprows,col)
292 item.append(cfcn(avgcol))
309 def dm (listoflists,criterion):
311 Returns rows from the passed list of lists that meet the criteria in 312 the passed criterion expression (a string as a function of x; e.g., 'x[3]>=9' 313 will return all rows where the 4th column>=9 and "x[2]=='N'" will return rows 314 with column 2 equal to the string 'N'). 316 Usage: dm (listoflists, criterion) 317 Returns: rows from listoflists that meet the specified criterion. 319 function =
'filter(lambda x: '+criterion+
',listoflists)' 320 lines = eval(function)
326 Returns the flattened version of a '2D' list. List-correlate to the a.ravel()() 327 method of NumPy arrays. 332 for i
in range(len(l)):
333 for j
in range(len(l[i])):
340 Returns the rows of a list of lists where col (from columnlist) = val 341 (from valuelist) for EVERY pair of values (columnlist[i],valuelists[i]). 342 len(columnlist) must equal len(valuelist). 344 Usage: linexand (listoflists,columnlist,valuelist) 345 Returns: the rows of listoflists where columnlist[i]=valuelist[i] for ALL i 347 if type(columnlist)
not in [ListType,TupleType]:
348 columnlist = [columnlist]
349 if type(valuelist)
not in [ListType,TupleType]:
350 valuelist = [valuelist]
352 for i
in range(len(columnlist)):
353 if type(valuelist[i])==StringType:
354 critval =
'\'' + valuelist[i] +
'\'' 356 critval = str(valuelist[i])
357 criterion = criterion +
' x['+str(columnlist[i])+
']=='+critval+
' and' 358 criterion = criterion[0:-3]
359 function =
'filter(lambda x: '+criterion+
',listoflists)' 360 lines = eval(function)
364 def linexor (listoflists,columnlist,valuelist):
366 Returns the rows of a list of lists where col (from columnlist) = val 367 (from valuelist) for ANY pair of values (colunmlist[i],valuelist[i[). 368 One value is required for each column in columnlist. If only one value 369 exists for columnlist but multiple values appear in valuelist, the 370 valuelist values are all assumed to pertain to the same column. 372 Usage: linexor (listoflists,columnlist,valuelist) 373 Returns: the rows of listoflists where columnlist[i]=valuelist[i] for ANY i 375 if type(columnlist)
not in [ListType,TupleType]:
376 columnlist = [columnlist]
377 if type(valuelist)
not in [ListType,TupleType]:
378 valuelist = [valuelist]
380 if len(columnlist) == 1
and len(valuelist) > 1:
381 columnlist = columnlist*len(valuelist)
382 for i
in range(len(columnlist)):
383 if type(valuelist[i])==StringType:
384 critval =
'\'' + valuelist[i] +
'\'' 386 critval = str(valuelist[i])
387 criterion = criterion +
' x['+str(columnlist[i])+
']=='+critval+
' or' 388 criterion = criterion[0:-2]
389 function =
'filter(lambda x: '+criterion+
',listoflists)' 390 lines = eval(function)
396 Returns a string composed of elements in inlist, with each element 397 separated by 'delimiter.' Used by function writedelimited. Use '\t' 400 Usage: linedelimited (inlist,delimiter) 404 if type(item) <> StringType:
406 outstr = outstr + item + delimiter
407 outstr = outstr[0:-1]
413 Returns a string composed of elements in inlist, with each element 414 right-aligned in columns of (fixed) colsize. 416 Usage: lineincols (inlist,colsize) where colsize is an integer 420 if type(item) <> StringType:
424 for i
in range(colsize-size):
425 outstr = outstr +
' ' 426 outstr = outstr + item
428 outstr = outstr + item[0:colsize+1]
434 Returns a string composed of elements in inlist, with each element 435 right-aligned in a column of width specified by a sequence colsizes. The 436 length of colsizes must be greater than or equal to the number of columns 439 Usage: lineincustcols (inlist,colsizes) 440 Returns: formatted string created from inlist 443 for i
in range(len(inlist)):
444 if type(inlist[i]) <> StringType:
445 item = str(inlist[i])
449 if size <= colsizes[i]:
450 for j
in range(colsizes[i]-size):
451 outstr = outstr +
' ' 452 outstr = outstr + item
454 outstr = outstr + item[0:colsizes[i]+1]
460 Converts a 1D list to a single long string for file output, using 461 the string.join function. 463 Usage: list2string (inlist,delimit=' ') 464 Returns: the string created from inlist 466 stringlist = map(makestr,inlist)
467 return string.join(stringlist,delimit)
472 Converts a 1D list to a 2D list (i.e., a list-of-lists). Useful when you 473 want to use put() to write a 1D list one item per line in the file. 475 Usage: makelol(inlist) 476 Returns: if l = [1,2,'hi'] then returns [[1],[2],['hi']] etc. 485 if type(x) <> StringType:
492 Prints a list of lists in columns, customized by the max size of items 493 within the columns (max size of items in col, plus 'extra' number of spaces). 494 Use 'dashes' or '\\n' in the list-of-lists to print dashes or blank lines, 497 Usage: printcc (lst,extra=2) 500 if type(lst[0])
not in [ListType,TupleType]:
503 list2print = copy.deepcopy(lst)
504 for i
in range(len(lst)):
505 if lst[i] == [
'\n']
or lst[i]==
'\n' or lst[i]==
'dashes' or lst[i]==
'' or lst[i]==[
'']:
506 rowstokill = rowstokill + [i]
508 for row
in rowstokill:
510 maxsize = [0]*len(list2print[0])
511 for col
in range(len(list2print[0])):
512 items =
colex(list2print,col)
513 items = map(makestr,items)
514 maxsize[col] = max(map(len,items)) + extra
516 if row == [
'\n']
or row ==
'\n' or row ==
'' or row == [
'']:
518 elif row == [
'dashes']
or row ==
'dashes':
519 dashes = [0]*len(maxsize)
520 for j
in range(len(maxsize)):
521 dashes[j] =
'-'*(maxsize[j]-2)
530 Prints a list of lists in columns of (fixed) colsize width, where 531 colsize is an integer. 533 Usage: printincols (listoflists,colsize) 536 for row
in listoflists:
541 def pl (listoflists):
543 Prints a list of lists, 1 list (row) at a time. 545 Usage: pl(listoflists) 548 for row
in listoflists:
564 Replaces all occurrences of 'oldval' with 'newval', recursively. 566 Usage: replace (inlst,oldval,newval) 569 for i
in range(len(lst)):
570 if type(lst[i])
not in [ListType,TupleType]:
571 if lst[i]==oldval: lst[i]=newval
573 lst[i] =
replace(lst[i],oldval,newval)
579 Changes the values in a list to a new set of values (useful when 580 you need to recode data from (e.g.) strings to numbers. cols defaults 581 to None (meaning all columns are recoded). 583 Usage: recode (inlist,listmap,cols=None) cols=recode cols, listmap=2D list 584 Returns: inlist with the appropriate values replaced with new ones 586 lst = copy.deepcopy(inlist)
588 if type(cols)
not in [ListType,TupleType]:
591 for row
in range(len(lst)):
593 idx =
colex(listmap,0).index(lst[row][col])
594 lst[row][col] = listmap[idx][1]
598 for row
in range(len(lst)):
599 for col
in range(len(lst)):
601 idx =
colex(listmap,0).index(lst[row][col])
602 lst[row][col] = listmap[idx][1]
610 Remaps values in a given column of a 2D list (listoflists). This requires 611 a criterion as a function of 'x' so that the result of the following is 612 returned ... map(lambda x: 'criterion',listoflists). 614 Usage: remap(listoflists,criterion) criterion=string 615 Returns: remapped version of listoflists 617 function =
'map(lambda x: '+criterion+
',listoflists)' 618 lines = eval(function)
624 Goes through each element in a 1D or 2D inlist, and applies the following 625 function to all elements of FloatType ... round(element,digits). 627 Usage: roundlist(inlist,digits) 628 Returns: list with rounded floats 630 if type(inlist[0])
in [IntType, FloatType]:
633 for i
in range(len(l)):
634 for j
in range(len(l[i])):
635 if type(l[i][j])==FloatType:
636 l[i][j] = round(l[i][j],digits)
642 Sorts a list of lists on the column(s) specified in the sequence 645 Usage: sortby(listoflists,sortcols) 646 Returns: sorted list, unchanged column ordering 648 newlist =
abut(
colex(listoflists,sortcols),listoflists)
651 numcols = len(sortcols)
654 crit =
'[' + str(numcols) +
':]' 655 newlist =
colex(newlist,crit)
661 Returns all unique items in the passed list. If the a list-of-lists 662 is passed, unique LISTS are found (i.e., items in the first dimension are 665 Usage: unique (inlist) 666 Returns: the unique elements (or rows) in inlist 670 if item
not in uniques:
676 Returns duplicate items in the FIRST dimension of the passed list. 678 Usage: duplicates (inlist) 681 for i
in range(len(inlist)):
682 if inlist[i]
in inlist[i+1:]:
683 dups.append(inlist[i])
689 Returns items that are NOT duplicated in the first dim of the passed list. 691 Usage: nonrepeats (inlist) 694 for i
in range(len(inlist)):
695 if inlist.count(inlist[i]) == 1:
696 nonrepeats.append(inlist[i])
720 def aabut (source, *args):
722 Like the |Stat abut command. It concatenates two arrays column-wise 723 and returns the result. CAUTION: If one array is shorter, it will be 724 repeated until it is as long as the other. 726 Usage: aabut (source, args) where args=any # of arrays 727 Returns: an array as long as the LONGEST array past, source appearing on the 728 'left', arrays in <args> attached on the 'right'. 730 if len(source.shape)==1:
732 source = N.resize(source,[source.shape[0],width])
734 width = source.shape[1]
736 if len(addon.shape)==1:
738 addon = N.resize(addon,[source.shape[0],width])
740 width = source.shape[1]
741 if len(addon) < len(source):
742 addon = N.resize(addon,[source.shape[0],addon.shape[1]])
743 elif len(source) < len(addon):
744 source = N.resize(source,[addon.shape[0],source.shape[1]])
745 source = N.concatenate((source,addon),1)
749 def acolex (a,indices,axis=1):
751 Extracts specified indices (a list) from passed array, along passed 752 axis (column extraction is default). BEWARE: A 1D array is presumed to be a 753 column-array (and that the whole array will be returned as a column). 755 Usage: acolex (a,indices,axis=1) 756 Returns: the columns of a specified by indices 758 if type(indices)
not in [ListType,TupleType,N.ndarray]:
760 if len(N.shape(a)) == 1:
761 cols = N.resize(a,[a.shape[0],1])
763 cols = N.take(a,indices,axis)
767 def acollapse (a,keepcols,collapsecols,fcn1=None,fcn2=None,cfcn=None):
769 Averages data in collapsecol, keeping all unique items in keepcols 770 (using unique, which keeps unique LISTS of column numbers), retaining 771 the unique sets of values in keepcols, the mean for each. If stderror or 772 N of the mean are desired, set either or both parameters to 1. 774 Usage: acollapse (a,keepcols,collapsecols,fcn1=None,fcn2=None,cfcn=None) 775 Returns: unique 'conditions' specified by the contents of columns specified 776 by keepcols, abutted with the mean(s) of column(s) specified by 779 def acollmean (inarray):
780 return N.sum(N.ravel(inarray))
782 if type(keepcols)
not in [ListType,TupleType,N.ndarray]:
783 keepcols = [keepcols]
784 if type(collapsecols)
not in [ListType,TupleType,N.ndarray]:
785 collapsecols = [collapsecols]
790 avgcol = acolex(a,collapsecols)
791 means = N.sum(avgcol)/float(len(avgcol))
796 test = N.array([
'N/A']*len(means))
797 means = aabut(means,test)
802 test = N.array([
'N/A']*len(means))
803 means = aabut(means,test)
806 if type(keepcols)
not in [ListType,TupleType,N.ndarray]:
807 keepcols = [keepcols]
808 values =
colex(a,keepcols)
813 if type(item)
not in [ListType,TupleType,N.ndarray]:
815 tmprows = alinexand(a,keepcols,item)
816 for col
in collapsecols:
817 avgcol = acolex(tmprows,col)
818 item.append(acollmean(avgcol))
833 new_a = N.array(newlist)
835 new_a = N.array(newlist,
'O')
839 def adm (a,criterion):
841 Returns rows from the passed list of lists that meet the criteria in 842 the passed criterion expression (a string as a function of x). 844 Usage: adm (a,criterion) where criterion is like 'x[2]==37' 846 function =
'filter(lambda x: '+criterion+
',a)' 847 lines = eval(function)
849 lines = N.array(lines)
851 lines = N.array(lines,dtype=
'O')
856 if type(x)==StringType:
862 def alinexand (a,columnlist,valuelist):
864 Returns the rows of an array where col (from columnlist) = val 865 (from valuelist). One value is required for each column in columnlist. 867 Usage: alinexand (a,columnlist,valuelist) 868 Returns: the rows of a where columnlist[i]=valuelist[i] for ALL i 870 if type(columnlist)
not in [ListType,TupleType,N.ndarray]:
871 columnlist = [columnlist]
872 if type(valuelist)
not in [ListType,TupleType,N.ndarray]:
873 valuelist = [valuelist]
875 for i
in range(len(columnlist)):
876 if type(valuelist[i])==StringType:
877 critval =
'\'' + valuelist[i] +
'\'' 879 critval = str(valuelist[i])
880 criterion = criterion +
' x['+str(columnlist[i])+
']=='+critval+
' and' 881 criterion = criterion[0:-3]
882 return adm(a,criterion)
885 def alinexor (a,columnlist,valuelist):
887 Returns the rows of an array where col (from columnlist) = val (from 888 valuelist). One value is required for each column in columnlist. 889 The exception is if either columnlist or valuelist has only 1 value, 890 in which case that item will be expanded to match the length of the 893 Usage: alinexor (a,columnlist,valuelist) 894 Returns: the rows of a where columnlist[i]=valuelist[i] for ANY i 896 if type(columnlist)
not in [ListType,TupleType,N.ndarray]:
897 columnlist = [columnlist]
898 if type(valuelist)
not in [ListType,TupleType,N.ndarray]:
899 valuelist = [valuelist]
901 if len(columnlist) == 1
and len(valuelist) > 1:
902 columnlist = columnlist*len(valuelist)
903 elif len(valuelist) == 1
and len(columnlist) > 1:
904 valuelist = valuelist*len(columnlist)
905 for i
in range(len(columnlist)):
906 if type(valuelist[i])==StringType:
907 critval =
'\'' + valuelist[i] +
'\'' 909 critval = str(valuelist[i])
910 criterion = criterion +
' x['+str(columnlist[i])+
']=='+critval+
' or' 911 criterion = criterion[0:-2]
912 return adm(a,criterion)
915 def areplace (a,oldval,newval):
917 Replaces all occurrences of oldval with newval in array a. 919 Usage: areplace(a,oldval,newval) 921 return N.where(a==oldval,newval,a)
924 def arecode (a,listmap,col='all'):
926 Remaps the values in an array to a new set of values (useful when 927 you need to recode data from (e.g.) strings to numbers as most stats 928 packages require. Can work on SINGLE columns, or 'all' columns at once. 931 Usage: arecode (a,listmap,col='all') 932 Returns: a version of array a where listmap[i][0] = (instead) listmap[i][1] 941 if type(pair[1]) == StringType
or work.dtype.char==
'O' or a.dtype.char==
'O':
942 work = N.array(work,dtype=
'O')
943 a = N.array(a,dtype=
'O')
944 for i
in range(len(work)):
948 return N.reshape(work,ashape)
950 return N.concatenate([a[:,0:col],work[:,N.newaxis],a[:,col+1:]],1)
952 work = N.where(work==pair[0],pair[1],work)
953 return N.concatenate([a[:,0:col],work[:,N.newaxis],a[:,col+1:]],1)
956 def arowcompare(row1, row2):
958 Compares two rows from an array, regardless of whether it is an 959 array of numbers or of python objects (which requires the cmp function). 960 @@@PURPOSE? 2007-11-26 962 Usage: arowcompare(row1,row2) 963 Returns: an array of equal length containing 1s where the two rows had 964 identical elements and 0 otherwise 967 if row1.dtype.char==
'O' or row2.dtype==
'O':
968 cmpvect = N.logical_not(abs(N.array(map(cmp,row1,row2))))
970 cmpvect = N.equal(row1,row2)
974 def arowsame(row1, row2):
976 Compares two rows from an array, regardless of whether it is an 977 array of numbers or of python objects (which requires the cmp function). 979 Usage: arowsame(row1,row2) 980 Returns: 1 if the two rows are identical, 0 otherwise. 982 cmpval = N.alltrue(arowcompare(row1,row2))
986 def asortrows(a,axis=0):
988 Sorts an array "by rows". This differs from the Numeric.sort() function, 989 which sorts elements WITHIN the given axis. Instead, this function keeps 990 the elements along the given axis intact, but shifts them 'up or down' 991 relative to one another. 993 Usage: asortrows(a,axis=0) 994 Returns: sorted version of a 996 return N.sort(a,axis=axis,kind=
'mergesort')
999 def aunique(inarray):
1001 Returns unique items in the FIRST dimension of the passed array. Only 1002 works on arrays NOT including string items. 1004 Usage: aunique (inarray) 1006 uniques = N.array([inarray[0]])
1007 if len(uniques.shape) == 1:
1008 for item
in inarray[1:]:
1009 if N.add.reduce(N.equal(uniques,item).ravel()) == 0:
1011 uniques = N.concatenate([uniques,N.array[N.newaxis,:]])
1013 uniques = N.concatenate([uniques,N.array([item])])
1015 if inarray.dtype.char !=
'O':
1016 for item
in inarray[1:]:
1017 if not N.sum(N.alltrue(N.equal(uniques,item),1)):
1019 uniques = N.concatenate( [uniques,item[N.newaxis,:]] )
1021 uniques = N.concatenate([uniques,N.array([item])])
1025 for item
in inarray[1:]:
1028 test = N.sum(abs(N.array(map(cmp,item,unq))))
1034 uniques = N.concatenate( [uniques,item[N.newaxis,:]] )
1036 uniques = N.concatenate([uniques,N.array([item])])
1040 def aduplicates(inarray):
1042 Returns duplicate items in the FIRST dimension of the passed array. Only 1043 works on arrays NOT including string items. 1045 Usage: aunique (inarray) 1047 inarray = N.array(inarray)
1048 if len(inarray.shape) == 1:
1050 inarray = inarray.tolist()
1051 for i
in range(len(inarray)):
1052 if inarray[i]
in inarray[i+1:]:
1053 dups.append(inarray[i])
1054 dups = aunique(dups)
1057 aslist = inarray.tolist()
1058 for i
in range(len(aslist)):
1059 if aslist[i]
in aslist[i+1:]:
1060 dups.append(aslist[i])
1062 dups = N.array(dups)
def colex(listoflists, cnums)
def linexor(listoflists, columnlist, valuelist)
def remap(listoflists, criterion)
def abut(source, args)
=========================== LIST FUNCTIONS ==========================
def roundlist(inlist, digits)
def printcc(lst, extra=2)
def recode(inlist, listmap, cols=None)
def collapse(listoflists, keepcols, collapsecols, fcn1=None, fcn2=None, cfcn=None)
def dm(listoflists, criterion)
def list2string(inlist, delimit=' ')
def replace(inlst, oldval, newval)
def simpleabut(source, addon)
def lineincols(inlist, colsize)
def linedelimited(inlist, delimiter)
def linexand(listoflists, columnlist, valuelist)
def sortby(listoflists, sortcols)
def printincols(listoflists, colsize)
def lineincustcols(inlist, colsizes)