Monday, December 21, 2015

Turbidostat, Double Colons (in Python), and Multivariate Mapping (in Python)

Today I Learned:
1) ...how this thing works: http://ift.tt/1V09oyZ

It's a simple, build-it-yourself, low-scale turbidostat, which is a device for growing cells (usually bacterial) at more-or-less constant density. We have one of these in the lab, but it wasn't... really... working.... I want to use one of these for a long-term experiment I have planned, so I and a colleague took a look at it. We figured out a few problems, and now it's time to run some tests and print out some replacement parts!

2) ...some new Python notation -- the double colon! In Python, the statement X[a:b] means the a- through b-th elements of list X. The statement X[a::b] means every bth element of X, starting with a. Rather handy for pulling the even or odd elements out of a list.

3) ...more about the map function* in Python. I use map a lot (more than I should), but I didn't know that you could map over functions with more than one argument -- you just pass multiple lists, one for each argument, and map will iterate over the first list for the first argument, the second list for the second, et.

Example:

def add(x,y):
    return x+y

a = [0,1,2,3,4]
b = [2,4,6,8,10]
print(map(add, a, b))

Output: [2, 5, 8, 1, 14]

* map takes a function and a list, applies the function to each element of the list, and returns a list of the results. For instance, if you have a function "square" that takes a number and squares it, then map(square, [0,1,2,3]) returns [0,1,4,9].

No comments:

Post a Comment