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