Package Bio :: Module listfns
[hide private]
[frames] | no frames]

Source Code for Module Bio.listfns

  1  # Copyright 2000 by Jeffrey Chang.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  """This provides useful general functions for working with lists (DEPRECATED). 
  7   
  8  This module is considered to be deprecated, and is likely to be removed in a 
  9  future release of Biopython.  Its C code implementation has already been 
 10  removed. Please get in touch via the mailing list if this will affect you. 
 11   
 12  Many of these functions can be avoided using the python set object. 
 13   
 14  Functions: 
 15  asdict        Make the list into a dictionary (for fast testing of membership). 
 16  items         Get one of each item in a list. 
 17  count         Count the number of times each item appears. 
 18  contents      Calculate percentage each item appears in a list. 
 19  itemindex     Make an index of the items in the list. 
 20  intersection  Get the items in common between 2 lists. 
 21  difference    Get the items in 1 list, but not the other. 
 22  indexesof     Get a list of the indexes of some items in a list. 
 23  take          Take some items from a list. 
 24   
 25  """ 
 26  import warnings 
 27  import Bio 
 28  warnings.warn("Bio.listfns and its C code equivalent Bio.clistfns are" \ 
 29                +" deprecated, and will be removed in a future release of"\ 
 30                +" Biopython.  If you want to continue to use this code,"\ 
 31                +" please get in contact with the Biopython developers via"\ 
 32                +" the mailing lists to avoid its permanent removal from"\ 
 33                +" Biopython. See also the Python built in set datatype.", \ 
 34                Bio.BiopythonDeprecationWarning) 
 35   
36 -def asdict(l):
37 """asdict(l) -> dictionary 38 39 Return a dictionary where the keys are the items in the list, with 40 arbitrary values. This is useful for quick testing of membership. 41 42 """ 43 return count(l)
44
45 -def items(l):
46 """items(l) -> list of items 47 48 Generate a list of one of each item in l. The items are returned 49 in arbitrary order. 50 51 """ 52 try: 53 return asdict(l).keys() 54 except TypeError, x: 55 if str(x).find("unhashable") == -1: 56 raise 57 # asdict failed because l is unhashable. Back up to a naive 58 # implementation. 59 l = l[:] 60 l.sort() 61 i = 0 62 while i < len(l)-1: 63 if l[i] == l[i+1]: 64 del l[i] 65 else: 66 i += 1 67 return l
68
69 -def count(items):
70 """count(items) -> dict of counts of each item 71 72 Count the number of times each item appears in a list of data. 73 74 """ 75 c = {} 76 for i in items: 77 c[i] = c.get(i, 0) + 1 78 return c
79
80 -def contents(items):
81 """contents(items) -> dict of item:percentage 82 83 Summarize the contents of the list in terms of the percentages of each 84 item. For example, if an item appears 3 times in a list with 10 items, 85 it is in 0.3 of the list. 86 87 """ 88 counts = count(items) 89 l = float(len(items)) 90 contents = {} 91 for i, c in counts.iteritems(): 92 contents[i] = c / l 93 return contents
94
95 -def intersection(l1, l2):
96 """intersection(l1, l2) -> list of common items 97 98 Return a list of the items in both l1 and l2. The list is in 99 arbitrary order. 100 101 """ 102 inter = [] 103 words1 = count(l1) 104 for w in l2: 105 if words1.has_key(w): 106 inter.append(w) 107 del words1[w] # don't add the same word twice 108 return inter
109
110 -def difference(l1, l2):
111 """difference(l1, l2) -> list of items in l1, but not l2 112 113 Return a list of the items in l1, but not l2. The list is in 114 arbitrary order. 115 116 """ 117 diff = [] 118 words2 = count(l2) 119 for w in l1: 120 if not words2.has_key(w): 121 diff.append(w) 122 words2[w] = 1 # don't add the same word twice 123 return diff
124
125 -def itemindex(l):
126 """itemindex(l) -> dict of item : index of item 127 128 Make an index of the items in the list. The dictionary contains 129 the items in the list as the keys, and the index of the first 130 occurrence of the item as the value. 131 132 """ 133 dict = {} 134 for i in range(len(l)): 135 if not dict.has_key(l[i]): 136 dict[l[i]] = i 137 return dict
138
139 -def indexesof(l, fn, opposite=0):
140 """indexesof(l, fn) -> list of indexes 141 142 Return a list of indexes i where fn(l[i]) is true. 143 144 """ 145 indexes = [] 146 for i in range(len(l)): 147 f = fn(l[i]) 148 if (not opposite and f) or (opposite and not f): 149 indexes.append(i) 150 return indexes
151
152 -def take(l, indexes):
153 """take(l, indexes) -> list of just the indexes from l""" 154 items = [] 155 for i in indexes: 156 items.append(l[i]) 157 return items
158
159 -def take_byfn(l, fn, opposite=0):
160 indexes = indexesof(l, fn, opposite=opposite) 161 return take(l, indexes)
162