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  warnings.warn("Bio.listfns and its C code equivalent Bio.clistfns are" \ 
 28                +" deprecated, and will be removed in a future release of"\ 
 29                +" Biopython.  If you want to continue to use this code,"\ 
 30                +" please get in contact with the Biopython developers via"\ 
 31                +" the mailing lists to avoid its permanent removal from"\ 
 32                +" Biopython. See also the Python built in set datatype.", \ 
 33                DeprecationWarning) 
 34   
35 -def asdict(l):
36 """asdict(l) -> dictionary 37 38 Return a dictionary where the keys are the items in the list, with 39 arbitrary values. This is useful for quick testing of membership. 40 41 """ 42 return count(l)
43
44 -def items(l):
45 """items(l) -> list of items 46 47 Generate a list of one of each item in l. The items are returned 48 in arbitrary order. 49 50 """ 51 try: 52 return asdict(l).keys() 53 except TypeError, x: 54 if str(x).find("unhashable") == -1: 55 raise 56 # asdict failed because l is unhashable. Back up to a naive 57 # implementation. 58 l = l[:] 59 l.sort() 60 i = 0 61 while i < len(l)-1: 62 if l[i] == l[i+1]: 63 del l[i] 64 else: 65 i += 1 66 return l
67
68 -def count(items):
69 """count(items) -> dict of counts of each item 70 71 Count the number of times each item appears in a list of data. 72 73 """ 74 c = {} 75 for i in items: 76 c[i] = c.get(i, 0) + 1 77 return c
78
79 -def contents(items):
80 """contents(items) -> dict of item:percentage 81 82 Summarize the contents of the list in terms of the percentages of each 83 item. For example, if an item appears 3 times in a list with 10 items, 84 it is in 0.3 of the list. 85 86 """ 87 counts = count(items) 88 l = float(len(items)) 89 contents = {} 90 for i, c in counts.items(): 91 contents[i] = c / l 92 return contents
93
94 -def intersection(l1, l2):
95 """intersection(l1, l2) -> list of common items 96 97 Return a list of the items in both l1 and l2. The list is in 98 arbitrary order. 99 100 """ 101 inter = [] 102 words1 = count(l1) 103 for w in l2: 104 if words1.has_key(w): 105 inter.append(w) 106 del words1[w] # don't add the same word twice 107 return inter
108
109 -def difference(l1, l2):
110 """difference(l1, l2) -> list of items in l1, but not l2 111 112 Return a list of the items in l1, but not l2. The list is in 113 arbitrary order. 114 115 """ 116 diff = [] 117 words2 = count(l2) 118 for w in l1: 119 if not words2.has_key(w): 120 diff.append(w) 121 words2[w] = 1 # don't add the same word twice 122 return diff
123
124 -def itemindex(l):
125 """itemindex(l) -> dict of item : index of item 126 127 Make an index of the items in the list. The dictionary contains 128 the items in the list as the keys, and the index of the first 129 occurrence of the item as the value. 130 131 """ 132 dict = {} 133 for i in range(len(l)): 134 if not dict.has_key(l[i]): 135 dict[l[i]] = i 136 return dict
137
138 -def indexesof(l, fn, opposite=0):
139 """indexesof(l, fn) -> list of indexes 140 141 Return a list of indexes i where fn(l[i]) is true. 142 143 """ 144 indexes = [] 145 for i in range(len(l)): 146 f = fn(l[i]) 147 if (not opposite and f) or (opposite and not f): 148 indexes.append(i) 149 return indexes
150
151 -def take(l, indexes):
152 """take(l, indexes) -> list of just the indexes from l""" 153 items = [] 154 for i in indexes: 155 items.append(l[i]) 156 return items
157
158 -def take_byfn(l, fn, opposite=0):
159 indexes = indexesof(l, fn, opposite=opposite) 160 return take(l, indexes)
161