



  

utils 0.1.1 documentation

 [utils](index.html#document-index)

* [List](index.html#document-list)

* [Stat](index.html#document-stat)

* [String](index.html#document-string)

* [Bio](index.html#document-bio)

  

 

[utils](index.html#document-index)

* [Docs](index.html#document-index) »

* utils 0.1.1 documentation

* [Edit on GitHub](https://github.com/sefakilic/utils/blob/master/docs/index.rst)

---

Welcome to utils’s documentation![¶](#welcome-to-utils-s-documentation "Permalink to this headline")

====================================================================================================

Contents:

List[¶](#module-utils.list "Permalink to this headline")

--------------------------------------------------------

List module implements list processing functions, mostly as named in

Data.List module of Haskell.

utils.list.concat(*xss*)[¶](#utils.list.concat "Permalink to this definition")

Concatenate a list of lists

```

>>> concat([[1,2], [3,4], [5]])

[1, 2, 3, 4, 5]

```

utils.list.concat\_map(*f*, *xs*)[¶](#utils.list.concat_map "Permalink to this definition")

Map a function over a list and concatenate the results.

```

>>> concat\_map(lambda x: [x]\*3, [1, 2, 3])

[1, 1, 1, 2, 2, 2, 3, 3, 3]

```

utils.list.cycle(*xs*)[¶](#utils.list.cycle "Permalink to this definition")

Given a list, return a circular list, the infinite repetition of the

original list.

```

>>> list(itertools.islice(cycle([1, 2, 3]), 10))

[1, 2, 3, 1, 2, 3, 1, 2, 3, 1]

```

utils.list.delete(*x*, *ys*)[¶](#utils.list.delete "Permalink to this definition")

Remove the first occurrence of x from its list argument.

```

>>> delete(1, [0, 1, 2, 3, 4])

[0, 2, 3, 4]

```

utils.list.diff(*xs*, *ys*)[¶](#utils.list.diff "Permalink to this definition")

List difference. In the result of diff(xs, ys) the first occurrence of

each element of ys in turn (if any) has been removed from xs. Thus

diff(xs+ys, xs) = ys

```

>>> diff([1, 2, 3, 1, 4, 2], [1, 2])

[3, 1, 4, 2]

>>> diff([], [1, 2])

[]

>>> diff([1, 2, 3], [])

[1, 2, 3]

```

utils.list.drop(*n*, *xs*)[¶](#utils.list.drop "Permalink to this definition")

Return the suffix of xs after first n elements, or [] if n > len(xs)

```

>>> drop(6, 'Hello world!')

'world!'

>>> drop(3, [1, 2])

[]

>>> drop(-1, [1, 2])

[1, 2]

```

utils.list.drop\_while(*pred*, *xs*)[¶](#utils.list.drop_while "Permalink to this definition")

Return the suffix remaining after take\_while(p, xs).

```

>>> drop\_while(lambda x: operator.lt(x, 3), [1, 2, 3, 4, 5, 1, 2, 3])

[3, 4, 5, 1, 2, 3]

>>> drop\_while(lambda x: operator.lt(x, 9), [1, 2, 3])

[]

>>> drop\_while(lambda x: operator.lt(x, 0), [1, 2, 3])

[1, 2, 3]

```

utils.list.drop\_while\_end(*pred*, *xs*)[¶](#utils.list.drop_while_end "Permalink to this definition")

Drop the largest suffix of the list in which the given predicate holds

for all elements.

```

>>> drop\_while\_end(lambda x: x>3, [1, 2, 3, 4, 5])

[1, 2, 3]

```

utils.list.elem(*x*, *xs*)[¶](#utils.list.elem "Permalink to this definition")

The list membership predicate.

```

>>> elem(1, [1, 2, 3])

True

>>> elem(4, [1, 2, 3])

False

```

utils.list.elem\_index(*p*, *xs*)[¶](#utils.list.elem_index "Permalink to this definition")

Return the index of the first element in the given list which is equal to

the query element, or None if there is no such element.

```

>>> elem\_index(2, [1, 2, 3, 2])

1

>>> elem\_index(4, [1, 2, 3, 2]) is None

True

```

utils.list.elem\_indices(*p*, *xs*)[¶](#utils.list.elem_indices "Permalink to this definition")

Return the indices of all elements equal to the query element, in

ascending order.

```

>>> elem\_indices(2, [1, 2, 3, 2])

[1, 3]

>>> elem\_indices(4, [1, 2, 3, 2]) == []

True

```

utils.list.find(*pred*, *xs*)[¶](#utils.list.find "Permalink to this definition")

Return the first element in the list matching the predicate and None if

there is no such element.

```

>>> find(lambda x: operator.lt(x, 3), [3, 2, 4])

1

>>> find(lambda x: operator.lt(x, 1), [3, 2, 4]) is None

True

```

utils.list.find\_index(*pred*, *xs*)[¶](#utils.list.find_index "Permalink to this definition")

Return the index of the first element in the list satisfying the

predicate, None if there is no such element.

```

>>> find\_index(lambda x: x==2, [1, 2, 3, 2])

1

>>> find\_index(lambda x: x==4, [1, 2, 3, 2]) is None

True

```

utils.list.find\_indices(*pred*, *xs*)[¶](#utils.list.find_indices "Permalink to this definition")

Return the indices of all elements satisfying the predicate, in ascending

order.

```

>>> find\_indices(lambda x: x==2, [1, 2, 3, 2])

[1, 3]

>>> find\_indices(lambda x: x==4, [1, 2, 3, 2])

[]

```

utils.list.foldl(*f*, *z*, *xs*)[¶](#utils.list.foldl "Permalink to this definition")

foldl, applied to a binary operator, a starting value (typically the

left-identity of the operator), and a list, reduces the list using the

binary operator, from left to right.

foldl(f, z, [x1, x2, x3]) == f(f(f(z, x1), x2), x3)

```

>>> foldl (operator.div, 64, [4, 2, 4])

2

```

utils.list.foldl1(*f*, *xs*)[¶](#utils.list.foldl1 "Permalink to this definition")

foldl1 is a variant of foldl that has no starting value argument, and

thus must be applied to non-empty lists.

```

>>> foldl1 (operator.div, [64, 4, 2, 4])

2

```

utils.list.foldr(*f*, *z*, *xs*)[¶](#utils.list.foldr "Permalink to this definition")

foldr, applied to a binary operator, a starting value (typically the

right-identity of the operator), and a list, reduces the list using the

binary operator, from right to left:

foldr(f, z, [x1,x2,x3]) = f(x1, f(x2, f(x3, z)))

```

>>> foldr(operator.div, 2, [8, 12, 24, 4])

8

```

utils.list.foldr1(*f*, *xs*)[¶](#utils.list.foldr1 "Permalink to this definition")

foldr1 is a variant of foldr that has no starting value argument, thus

must be applied to non-empty lists.

```

>>> foldr1(operator.div, [8, 12, 24, 4])

4

```

utils.list.group(*xs*)[¶](#utils.list.group "Permalink to this definition")

The group function takes a list and returns a list of lists such that

the concatenation of the result is equal to the argument. Moreover, each

sublist in the result contains only equal elements.

```

>>> group([1, 2, 2, 3, 3, 3, 1, 2, 2])

[[1], [2, 2], [3, 3, 3], [1], [2, 2]]

```

utils.list.head(*xs*)[¶](#utils.list.head "Permalink to this definition")

Extract the first element of a list, which must be non-empty.

```

>>> head([1, 2, 3])

1

```

utils.list.init(*xs*)[¶](#utils.list.init "Permalink to this definition")

Return all the elements of a list except the last one. The list must be

non-empty.

```

>>> init([1, 2, 3])

[1, 2]

```

utils.list.inits(*xs*)[¶](#utils.list.inits "Permalink to this definition")

Return all initial segments of the argument, shortest first.

```

>>> inits('abc')

[[], 'a', 'ab', 'abc']

>>> inits([1,2,3])

[[], [1], [1, 2], [1, 2, 3]]

```

utils.list.intercalate(*xs*, *xss*)[¶](#utils.list.intercalate "Permalink to this definition")

intercalate(xs, xss) is equivalent to concat(intersperse xs xss). It

inserts the list xs in between the lists in xss and concatenates the

result.

```

>>> intercalate([0, 0], [[1, 2, 3], [4, 5, 6], [7, 8]])

[1, 2, 3, 0, 0, 4, 5, 6, 0, 0, 7, 8]

```

utils.list.intersect(*xs*, *ys*)[¶](#utils.list.intersect "Permalink to this definition")

Return the intersection of two lists.

If the first list contains duplicates, so will the result.

```

>>> intersect([1, 2, 3, 4], [5, 4, 3])

[3, 4]

>>> intersect([1, 1, 2, 2, 3, 3, 4, 4], [5, 4, 3])

[3, 3, 4, 4]

```

utils.list.intersperse(*sep*, *xs*)[¶](#utils.list.intersperse "Permalink to this definition")

Take an element and a list and ‘intersperse’ that element between the

elements of the list.

```

>>> intersperse(0, [1,2,3])

[1, 0, 2, 0, 3]

>>> intersperse(0, [])

[]

```

utils.list.is\_infix\_of(*xs*, *ys*)[¶](#utils.list.is_infix_of "Permalink to this definition")

Take two lists and return True if the first is the infix of the second.

```

>>> is\_infix\_of([2, 3, 4], [1, 2, 3, 4, 5])

True

>>> is\_infix\_of([1, 2, 5], [1, 2, 3, 4, 5])

False

```

utils.list.is\_prefix\_of(*xs*, *ys*)[¶](#utils.list.is_prefix_of "Permalink to this definition")

Take two lists and return True if the first list is the prefix of the

second.

```

>>> is\_prefix\_of([1, 2, 3], [1, 2, 3, 4, 5])

True

>>> is\_prefix\_of([2, 3, 4], [1, 2, 3, 4, 5])

False

```

utils.list.is\_suffix\_of(*xs*, *ys*)[¶](#utils.list.is_suffix_of "Permalink to this definition")

Take two lists and return True if the first is the suffix of the second.

```

>>> is\_suffix\_of([3, 4, 5], [1, 2, 3, 4, 5])

True

>>> is\_suffix\_of([2, 3, 4], [1, 2, 3, 4, 5])

False

```

utils.list.iterate(*f*, *x*)[¶](#utils.list.iterate "Permalink to this definition")

Return an infinite list of repeated applications of f to x.

iterate(f, x) = [x, f(x), f(f(x)), f(f(f(x))), ...]

```

>>> list(itertools.islice(iterate(lambda x: 2\*x, 1), 3))

[2, 4, 8]

```

utils.list.lall(*f*, *xs*)[¶](#utils.list.lall "Permalink to this definition")

Applied to a predicate and a list, ‘all’ determines if all elements of

the list satisfy the predicate.

```

>>> lall(lambda x: operator.le(x, 5), [3, 4, 5, 6])

False

>>> lall(lambda x: operator.le(x, 9), [3, 4, 5, 6])

True

```

utils.list.land(*bs*)[¶](#utils.list.land "Permalink to this definition")

land returns the conjunction of a Boolean list.

```

>>> land([True, False, True])

False

>>> land([True, True])

True

>>> land([])

True

```

utils.list.lany(*f*, *xs*)[¶](#utils.list.lany "Permalink to this definition")

Applied to a predicate and a list, ‘any’ determines if any element of the

list satisfies the predicate.

```

>>> lany(lambda x: operator.ge(x, 5), [3, 4, 5, 6])

True

>>> lany(lambda x: operator.ge(x, 9), [3, 4, 5, 6])

False

```

utils.list.last(*xs*)[¶](#utils.list.last "Permalink to this definition")

Extract the last element of a list, which must be non-empty.

```

>>> last([1, 2, 3])

3

```

utils.list.lbreak(*pred*, *xs*)[¶](#utils.list.lbreak "Permalink to this definition")

lbreak, applied to a predicate pred and a list xs, returns a tuple where

first element is the longest prefix (possibly empty) of xs of elements

that do \_not\_ satisfy p and the second element is the remainder of the list.

```

>>> lbreak(lambda x: x>3, [1, 2, 3, 4, 1, 2, 3, 4])

([1, 2, 3], [4, 1, 2, 3, 4])

>>> lbreak(lambda x: x<9, [1, 2, 3])

([], [1, 2, 3])

>>> lbreak(lambda x: x>9, [1, 2, 3])

([1, 2, 3], [])

```

utils.list.length(*xs*)[¶](#utils.list.length "Permalink to this definition")

Return the length of the list.

```

>>> length([1, 2, 3])

3

```

utils.list.lookup(*key*, *assocs*)[¶](#utils.list.lookup "Permalink to this definition")

Look up a key in an association list.

```

>>> lookup('c', [('a', 0), ('b', 1), ('c', 2)])

2

>>> lookup('f', [('a', 0), ('b', 1), ('c', 2)]) is None

True

```

utils.list.lor(*bs*)[¶](#utils.list.lor "Permalink to this definition")

lor returns the disjunction of a Boolean list.

```

>>> lor([True, False, True])

True

>>> lor([True, True])

True

>>> lor([])

False

```

utils.list.lsum(*nums*)[¶](#utils.list.lsum "Permalink to this definition")

Compute the sum of a list of numbers.

>>> lsum([1, 2, 3])

6

utils.list.map\_accum\_l(*f*, *z*, *xs*)[¶](#utils.list.map_accum_l "Permalink to this definition")

map\_accum\_l behaves like a combination of map and foldl; it applies a

function to each element of a list, passing an accumulating parameter from

left to right, and returning a final value for this accumulator together

with the new list.

```

>>> map\_accum\_l(lambda x, y: (x, x\*y), 5, [9, 6, 3])

(5, [45, 30, 15])

>>> map\_accum\_l(lambda x, y: (x+y, x\*y), 5, [2, 4, 8])

(19, [10, 28, 88])

>>> map\_accum\_l(lambda x, y: (x+y, y), 5, [2, 4, 8])

(19, [2, 4, 8])

>>> map\_accum\_l(lambda x, y: (y, y), 5, [2, 4, 8])

(8, [2, 4, 8])

>>> map\_accum\_l(lambda x, y: (x, x), 5, [2, 4, 8])

(5, [5, 5, 5])

```

utils.list.map\_accum\_r(*f*, *z*, *xs*)[¶](#utils.list.map_accum_r "Permalink to this definition")

map\_accum\_r function behaves like a combination of ‘map’ and ‘foldr’;

it applies a function to each element of a list, passing an accumulating

parameter from right to left, and returning a final value of this

accumulator together with the new list.

```

>>> map\_accum\_r(lambda x,y: (x, x\*y), 5, [9, 6, 3])

(5, [45, 30, 15])

>>> map\_accum\_r(lambda x,y: (x+y, x\*y), 5, [2, 4, 8])

(19, [34, 52, 40])

>>> map\_accum\_r(lambda x,y: (x+y, y), 5, [2, 4, 8])

(19, [2, 4, 8])

```

utils.list.maximum(*xs*)[¶](#utils.list.maximum "Permalink to this definition")

Return the maximum value from the list, which must be non-empty and of an

ordered type.

```

>>> maximum([1, 3, 4, 2])

4

```

utils.list.minimum(*xs*)[¶](#utils.list.minimum "Permalink to this definition")

Return the minimum value from the list, which must be non-empty and of an

ordered type.

```

>>> minimum([1, 3, 4, 2])

1

```

utils.list.not\_elem(*x*, *xs*)[¶](#utils.list.not_elem "Permalink to this definition")

Negation of elem.

```

>>> not\_elem(1, [1, 2, 3])

False

>>> not\_elem(4, [1, 2, 3])

True

```

utils.list.nub(*xs*)[¶](#utils.list.nub "Permalink to this definition")

Remove duplicates from a list.

```

>>> nub([1, 2, 1, 3, 2, 1, 2, 3])

[1, 2, 3]

```

utils.list.nub\_by(*eqtest*, *xs*)[¶](#utils.list.nub_by "Permalink to this definition")

Behave just like nub, except use a user-supplied equality predicate

instead of == function.

```

>>> nub\_by(lambda x, y: (x>0) == (y>0), [1, 2, -1, 2, -3, 4])

[1, -1]

```

utils.list.null(*xs*)[¶](#utils.list.null "Permalink to this definition")

Test whether a list is empty.

```

>>> null([1, 2, 3])

False

>>> null([])

True

```

utils.list.partition(*pred*, *xs*)[¶](#utils.list.partition "Permalink to this definition")

Return a pair of lists of elements which do and don’t satisfy the

predicate.

```

>>> partition(lambda x: x<3, [1, 2, 3, 4, 5])

([1, 2], [3, 4, 5])

```

utils.list.permutations(*xs*, *r=None*)[¶](#utils.list.permutations "Permalink to this definition")

The permutations function returns the list of all permutations of the

argument of length r If r is not specified or is None, it defaults to the

length of the list.

```

>>> permutations([1,2,3])

[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

>>> permutations([1,2,3], 2)

[[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]

```

utils.list.product(*nums*)[¶](#utils.list.product "Permalink to this definition")

Compute the product of a list of numbers.

```

>>> product([1, 2, 3, 4])

24

```

utils.list.repeat(*x*)[¶](#utils.list.repeat "Permalink to this definition")

Return an infinite list, with x the value of every element.

```

>>> list(itertools.islice(repeat(3), 4))

[3, 3, 3, 3]

```

utils.list.replicate(*n*, *x*)[¶](#utils.list.replicate "Permalink to this definition")

Return a list of length given by the first argument and the items having

value of the second argument.

```

>>> replicate(3, 5)

[5, 5, 5]

```

utils.list.reverse(*xs*)[¶](#utils.list.reverse "Permalink to this definition")

Return the elements of xs in reverse order.

```

>>> reverse([1, 2, 3])

[3, 2, 1]

```

utils.list.scanl(*f*, *z*, *xs*)[¶](#utils.list.scanl "Permalink to this definition")

scanl is similar to foldl, but returns a list of successive reduced

values from the left.

scanl(f, z, [x1,x2,x3]) ==

[z, f(z, x1), f(f(z, x1), x2), f(f(f(z, x1), x2), x3)]

```

>>> scanl(operator.div, 64, [4, 2, 4])

[64, 16, 8, 2]

```

utils.list.scanl1(*f*, *xs*)[¶](#utils.list.scanl1 "Permalink to this definition")

scanl1 is a variant of scanl that has no starting value argument

```

>>> scanl1(operator.div, [64, 4, 2, 4])

[64, 16, 8, 2]

```

utils.list.scanr(*f*, *z*, *xs*)[¶](#utils.list.scanr "Permalink to this definition")

scanr is the right-to-left dual of scanl.

```

>>> scanr(operator.add, 5, [1, 2, 3, 4])

[15, 14, 12, 9, 5]

>>> scanr(operator.div, 2, [8, 12, 24, 4])

[8, 1, 12, 2, 2]

```

utils.list.scanr1(*f*, *xs*)[¶](#utils.list.scanr1 "Permalink to this definition")

scanr1 is a variant of scanr that has no starting value argument.

```

>>> scanr1(operator.add, [1, 2, 3, 4])

[10, 9, 7, 4]

>>> scanr1(operator.div, [8, 12, 24, 2])

[8, 1, 12, 2]

```

utils.list.shuffle(*xs*)[¶](#utils.list.shuffle "Permalink to this definition")

Return a shuffled copy of the list.

```

>>> random.seed(1); shuffle([1, 2, 3, 4, 5])

[2, 5, 3, 4, 1]

```

utils.list.span(*pred*, *xs*)[¶](#utils.list.span "Permalink to this definition")

span, applied to a predicate pred and a list xs, returns a tuple where

the first element is longest prefix (possibly empty) of xs of elements

that satisfy pred and the second element is the remainder of the list.

```

>>> span(lambda x: x<3, [1, 2, 3, 4, 1, 2, 3, 4])

([1, 2], [3, 4, 1, 2, 3, 4])

>>> span(lambda x: x<9, [1, 2, 3])

([1, 2, 3], [])

>>> span(lambda x: x<0, [1, 2, 3])

([], [1, 2, 3])

```

utils.list.split\_at(*n*, *xs*)[¶](#utils.list.split_at "Permalink to this definition")

Return a tuple where first element is xs prefix of length n and the

second element is the remainder of the list.

```

>>> split\_at(3, [1,2,3,4,5])

([1, 2, 3], [4, 5])

```

utils.list.strip\_prefix(*prefix*, *xs*)[¶](#utils.list.strip_prefix "Permalink to this definition")

Drop the given prefix from the list.

```

>>> strip\_prefix('foo', 'foobar')

'bar'

>>> strip\_prefix('foo', 'foo')

''

>>> strip\_prefix('barfoo', 'foo')

'foo'

```

utils.list.subsequences(*xs*)[¶](#utils.list.subsequences "Permalink to this definition")

Return the list of all subsequences of the argument.

```

>>> subsequences([1,2,3])

[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

```

utils.list.tail(*xs*)[¶](#utils.list.tail "Permalink to this definition")

Extract the elements after the head of a list, which must be non-empty.

```

>>> tail([1, 2, 3])

[2, 3]

```

utils.list.tails(*xs*)[¶](#utils.list.tails "Permalink to this definition")

Return all tails of the argument, longest first.

```

>>> tails([1, 2, 3])

[[1, 2, 3], [2, 3], [3], []]

```

utils.list.take(*n*, *xs*)[¶](#utils.list.take "Permalink to this definition")

Return the prefix of xs of length n, or xs itself if n > len(xs)

```

>>> take(5, 'hello world!')

'hello'

>>> take(3, [1, 2, 3, 4, 5])

[1, 2, 3]

>>> take(3, [1])

[1]

```

utils.list.take\_while(*pred*, *xs*)[¶](#utils.list.take_while "Permalink to this definition")

Return the longest prefix (possibly empty) of xs of elements that satisfy

pred.

```

>>> take\_while(lambda x: operator.lt(x, 3), [1, 2, 3, 4, 1, 2, 3, 4])

[1, 2]

>>> take\_while(lambda x: operator.lt(x, 9), [1, 2, 3])

[1, 2, 3]

>>> take\_while(lambda x: operator.lt(x, 0), [1, 2, 3])

[]

```

utils.list.transpose(*xss*)[¶](#utils.list.transpose "Permalink to this definition")

Transpose the rows and columns of xss.

```

>>> transpose([[1,2,3], [4,5,6]])

[[1, 4], [2, 5], [3, 6]]

```

utils.list.union(*xs*, *ys*)[¶](#utils.list.union "Permalink to this definition")

Return the list union of the two lists.

Duplicates, and elements of the first list, are removed from the second

list, but if the first list contains duplicates, so will the result.

```

>>> union([1, 2, 3], [4, 5])

[1, 2, 3, 4, 5]

>>> union([1, 2, 3], [3, 2, 1, 0])

[1, 2, 3, 0]

>>> union([1, 2, 2], [1, 2, 3])

[1, 2, 2, 3]

```

utils.list.unzip(*zipped*)[¶](#utils.list.unzip "Permalink to this definition")

Transform a list of tuples into a list of first components, second

components, and so on.

```

>>> unzip(zip([1, 2, 3], [4, 5, 6]))

[[1, 2, 3], [4, 5, 6]]

>>> unzip(zip([1, 2, 3], [4, 5, 6], [7, 8, 9]))

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

```

utils.list.zip\_with(*f*, *\*lists*)[¶](#utils.list.zip_with "Permalink to this definition")

Generalize zip by zipping with the function given as the first argument,

instead of tupling function.

```

>>> zip\_with(operator.add, [1, 2, 3], [4, 5, 6])

[5, 7, 9]

>>> zip\_with(lambda x,y,z: x\*y\*z, [1, 2, 3], [4, 5, 6], [7, 8, 9])

[28, 80, 162]

```

Stat[¶](#module-utils.stat "Permalink to this headline")

--------------------------------------------------------

Math and statistics related utility functions

utils.stat.log2(*x*)[¶](#utils.stat.log2 "Permalink to this definition")

Binary logarithm

```

>>> log2(2)

1.0

```

utils.stat.mean(*xs*)[¶](#utils.stat.mean "Permalink to this definition")

Mean of a list of numbers.

```

>>> mean([1,2,3,4])

2.5

```

utils.stat.mean\_and\_sd(*xs*)[¶](#utils.stat.mean_and_sd "Permalink to this definition")

Mean and standard deviation as tuple.

```

>>> map(lambda x: round(x, 2), mean\_and\_sd([1, 2, 3, 4, 5]))

[3.0, 1.58]

```

utils.stat.median(*xs*)[¶](#utils.stat.median "Permalink to this definition")

Median of a list of numbers

```

>>> median([3, 2, 1, 4, 5])

3

>>> median([3, 2, 1, 4])

2.5

```

utils.stat.normalize(*xs*)[¶](#utils.stat.normalize "Permalink to this definition")

utils.stat.pearson\_correlation(*x*, *y*)[¶](#utils.stat.pearson_correlation "Permalink to this definition")

Pearson product-moment correlation coefficient

utils.stat.sample(*xs*)[¶](#utils.stat.sample "Permalink to this definition")

utils.stat.se(*xs*, *correct=True*)[¶](#utils.stat.se "Permalink to this definition")

Standard error of a list of numbers.

```

>>> round(se([1, 2, 3, 4, 5]), 2)

0.71

>>> round(se([1, 2, 3, 4, 5], correct=False), 2)

0.63

```

utils.stat.std(*xs*, *correct=True*)[¶](#utils.stat.std "Permalink to this definition")

Standard deviation of a list of numbers.

```

>>> round(sd([1, 2, 3, 4, 5]), 2)

1.58

>>> round(sd([1, 2, 3, 4, 5], correct=False), 2)

1.41

```

utils.stat.variance(*xs*, *correct=True*)[¶](#utils.stat.variance "Permalink to this definition")

Variance of a list of numbers.

```

>>> variance([1, 2, 3, 4, 5])

2.5

>>> variance([1, 2, 3, 4, 5], correct=False)

2.0

```

String[¶](#string "Permalink to this headline")

-----------------------------------------------

utils.string.lines(*s*)[¶](#utils.string.lines "Permalink to this definition")

Break a string up into a list of strings at newline characters. The

resulting strings do not contain newlines.

```

>>> lines("string\nwith\nnewline")

['string', 'with', 'newline']

```

utils.string.unlines(*ls*)[¶](#utils.string.unlines "Permalink to this definition")

Inverse operation to lines.

```

>>> unlines(['string', 'with', 'newline'])

'string\nwith\nnewline'

```

utils.string.unwords(*ws*)[¶](#utils.string.unwords "Permalink to this definition")

Inverse operation to words. Join words with separating spaces.

```

>>> unwords(['two', 'words'])

'two words'

```

utils.string.words(*s*)[¶](#utils.string.words "Permalink to this definition")

Break a string into a list of words, which were delimited by whitespace.

```

>>> words("two words")

['two', 'words']

```

Bio[¶](#module-utils.bio "Permalink to this headline")

------------------------------------------------------

Utility functions, mostly using biopython

utils.bio.base\_freq(*seq*)[¶](#utils.bio.base_freq "Permalink to this definition")

Base frequencies of a sequence

utils.bio.build\_motif(*sites*, *bg=None*)[¶](#utils.bio.build_motif "Permalink to this definition")

Given a collection of sites, return a Biopython motif object.

utils.bio.complement(*seq*)[¶](#utils.bio.complement "Permalink to this definition")

Return the complement of a sequence

utils.bio.get\_genome(*accession*, *data\_dir*)[¶](#utils.bio.get_genome "Permalink to this definition")

Read the genome from file if available, otherwise retrieve genome record

from NCBI.

utils.bio.get\_motif\_sequences(*motif*)[¶](#utils.bio.get_motif_sequences "Permalink to this definition")

Return sequences of a motif

utils.bio.get\_org\_name(*rec*)[¶](#utils.bio.get_org_name "Permalink to this definition")

Return the organism name, given the genome record.

utils.bio.get\_protein(*accession*)[¶](#utils.bio.get_protein "Permalink to this definition")

Get protein record from NCBI

utils.bio.get\_pubmed(*pmid*)[¶](#utils.bio.get_pubmed "Permalink to this definition")

Retrieve pubmed publication from NCBI database

utils.bio.pssm\_threshold(*motif*, *alpha\_n=0.05*, *n=500*, *background=None*)[¶](#utils.bio.pssm_threshold "Permalink to this definition")

Given a motif, return the pssm scoring threshold th such that the

probability of observing a site in a n (e.g. 500) bp sequence from

background distribution having a score at least th is alpha\_n

(e.g. 0.05).

utils.bio.read\_fasta(*fasta\_file*)[¶](#utils.bio.read_fasta "Permalink to this definition")

Read fasta file and return the list of records, consisting of description

and sequence

utils.bio.read\_seqs\_from\_fasta(*fasta\_file*)[¶](#utils.bio.read_seqs_from_fasta "Permalink to this definition")

Read sequences from the file

utils.bio.read\_seqs\_from\_text\_file(*text\_file*)[¶](#utils.bio.read_seqs_from_text_file "Permalink to this definition")

Read sequences from a text file, one sequence per line.

utils.bio.reverse\_complement(*seq*)[¶](#utils.bio.reverse_complement "Permalink to this definition")

Reverse complement of a sequence

utils.bio.run\_meme(*seqs*, *outdir*, *meme\_settings*)[¶](#utils.bio.run_meme "Permalink to this definition")

Run MEME for motif discovery.

meme\_settings is a dictionary that contains all MEME parameters.

Return a MEME record object as defined in Bio-python module.

utils.bio.weblogo(*motif*, *fname=None*, *\*\*kwargs*)[¶](#utils.bio.weblogo "Permalink to this definition")

Build the weblogo given a motif. If fname argument is None, just display

the weblogo. Otherwise, write the logo to the file.

