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.
6 """This provides useful general functions for working with lists (OBSOLETE).
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.
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.
27 """asdict(l) -> dictionary
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.
36 """items(l) -> list of items
38 Generate a list of one of each item in l. The items are returned
43 return asdict(l).keys()
45 if str(x).find("unhashable") == -1:
47 # asdict failed because l is unhashable. Back up to a naive
60 """count(items) -> dict of counts of each item
62 Count the number of times each item appears in a list of data.
67 c[i] = c.get(i, 0) + 1
71 """contents(items) -> dict of item:percentage
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.
81 for i, c in counts.items():
85 def intersection(l1, l2):
86 """intersection(l1, l2) -> list of common items
88 Return a list of the items in both l1 and l2. The list is in
97 del words1[w] # don't add the same word twice
100 def difference(l1, l2):
101 """difference(l1, l2) -> list of items in l1, but not l2
103 Return a list of the items in l1, but not l2. The list is in
110 if not words2.has_key(w):
112 words2[w] = 1 # don't add the same word twice
116 """itemindex(l) -> dict of item : index of item
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.
124 for i in range(len(l)):
125 if not dict.has_key(l[i]):
129 def indexesof(l, fn, opposite=0):
130 """indexesof(l, fn) -> list of indexes
132 Return a list of indexes i where fn(l[i]) is true.
136 for i in range(len(l)):
138 if (not opposite and f) or (opposite and not f):
142 def take(l, indexes):
143 """take(l, indexes) -> list of just the indexes from l"""
149 def take_byfn(l, fn, opposite=0):
150 indexes = indexesof(l, fn, opposite=opposite)
151 return take(l, indexes)
153 # Try and load C implementations of functions. If I can't,
154 # then just ignore and use the pure python implementations.
156 from clistfns import *