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. 
  7   
  8  Functions: 
  9  asdict        Make the list into a dictionary (for fast testing of membership). 
 10  items         Get one of each item in a list. 
 11  count         Count the number of times each item appears. 
 12  contents      Calculate percentage each item appears in a list. 
 13  itemindex     Make an index of the items in the list. 
 14  intersection  Get the items in common between 2 lists. 
 15  difference    Get the items in 1 list, but not the other. 
 16  indexesof     Get a list of the indexes of some items in a list. 
 17  take          Take some items from a list. 
 18   
 19  """ 
 20   
21 -def asdict(l):
22 """asdict(l) -> dictionary 23 24 Return a dictionary where the keys are the items in the list, with 25 arbitrary values. This is useful for quick testing of membership. 26 27 """ 28 return count(l)
29
30 -def items(l):
31 """items(l) -> list of items 32 33 Generate a list of one of each item in l. The items are returned 34 in arbitrary order. 35 36 """ 37 try: 38 return asdict(l).keys() 39 except TypeError, x: 40 if str(x).find("unhashable") == -1: 41 raise 42 # asdict failed because l is unhashable. Back up to a naive 43 # implementation. 44 l = l[:] 45 l.sort() 46 i = 0 47 while i < len(l)-1: 48 if l[i] == l[i+1]: 49 del l[i] 50 else: 51 i += 1 52 return l
53
54 -def count(items):
55 """count(items) -> dict of counts of each item 56 57 Count the number of times each item appears in a list of data. 58 59 """ 60 c = {} 61 for i in items: 62 c[i] = c.get(i, 0) + 1 63 return c
64
65 -def contents(items):
66 """contents(items) -> dict of item:percentage 67 68 Summarize the contents of the list in terms of the percentages of each 69 item. For example, if an item appears 3 times in a list with 10 items, 70 it is in 0.3 of the list. 71 72 """ 73 counts = count(items) 74 l = float(len(items)) 75 contents = {} 76 for i, c in counts.items(): 77 contents[i] = c / l 78 return contents
79
80 -def intersection(l1, l2):
81 """intersection(l1, l2) -> list of common items 82 83 Return a list of the items in both l1 and l2. The list is in 84 arbitrary order. 85 86 """ 87 inter = [] 88 words1 = count(l1) 89 for w in l2: 90 if words1.has_key(w): 91 inter.append(w) 92 del words1[w] # don't add the same word twice 93 return inter
94
95 -def difference(l1, l2):
96 """difference(l1, l2) -> list of items in l1, but not l2 97 98 Return a list of the items in l1, but not l2. The list is in 99 arbitrary order. 100 101 """ 102 diff = [] 103 words2 = count(l2) 104 for w in l1: 105 if not words2.has_key(w): 106 diff.append(w) 107 words2[w] = 1 # don't add the same word twice 108 return diff
109
110 -def itemindex(l):
111 """itemindex(l) -> dict of item : index of item 112 113 Make an index of the items in the list. The dictionary contains 114 the items in the list as the keys, and the index of the first 115 occurrence of the item as the value. 116 117 """ 118 dict = {} 119 for i in range(len(l)): 120 if not dict.has_key(l[i]): 121 dict[l[i]] = i 122 return dict
123
124 -def indexesof(l, fn, opposite=0):
125 """indexesof(l, fn) -> list of indexes 126 127 Return a list of indexes i where fn(l[i]) is true. 128 129 """ 130 indexes = [] 131 for i in range(len(l)): 132 f = fn(l[i]) 133 if (not opposite and f) or (opposite and not f): 134 indexes.append(i) 135 return indexes
136
137 -def take(l, indexes):
138 """take(l, indexes) -> list of just the indexes from l""" 139 items = [] 140 for i in indexes: 141 items.append(l[i]) 142 return items
143
144 -def take_byfn(l, fn, opposite=0):
145 indexes = indexesof(l, fn, opposite=opposite) 146 return take(l, indexes)
147 148 # Try and load C implementations of functions. If I can't, 149 # then just ignore and use the pure python implementations. 150 try: 151 from clistfns import * 152 except ImportError: 153 pass 154