Just like in Snap!, Python allows us to operate on all of the items of a list by using Higher Order Functions (HOFs). In python we can perform a map operation that squares all of the numbers in a list by using the following syntax:
|
|
Above you can see that the python code is very similar to the Snap! code. Instead of having a multiply function with two blanks, as in the Snap! version of map, we first define a square
function and pass it as an argument to the map function. We could also create an anonymous function by using the lambda
keyword, which acts a lot like the ringify command in Snap! We won't use lambda
in this lab, but you'll see it plenty in 61A.
The map
function in Python is actually not used very often. Instead, a closely related construct called a list comprehension is often used instead. An example is given below.
|
|
[x * x for x in my_list]
may look very strange at first, but once you've used this idea a few times, you'll realize it's quite powerful and easy to use. As before, you'll see that the python code shown is still fairly similar to the Snap! code. The differences are:
x * x
and we then specify that x in my_list
.
map
function we are now using a special purpose syntax used specifically for mapping functions over lists.
my_list
by itself, producing the effect of squaring each number. And like the map
function, we produce a new list without changing the original list.
The syntax for a list comprehension is as follows:
>>> [function(x) for x in my_list]
This syntax can be confusing at first because it looks like we're writing a bunch of code on one line inside of a list. Don't be fooled! Again, list comprehensions look weird, but they'll make lots of sense after you use them.
In Python, the keep function is called filter
. However, in practice, most people use list comprehensions, so we will not show any examples of using filter
.
|
|
Or more generally, if we want to keep only items corresponding to some predicate, we'd use the following. Keep in mind that a predicate is simply a function that returns True
or False
.
>>> [x for x in my_list if predicate(x)]
List Comprehensions can be used to perform map and keep at the same time. We've also seen how to do this in Snap!, as shown to the right. Observe that the list comprehension is more concise.
|
|
Write the helper function substitute_base
that takes as input a string representing a DNA sequence, a base to be substituted (old), and a base that is to take the place of the base being substituted (new). Old and New could be any letter, so make sure to keep your code general! This function should return a string with the proper base substituted:
>>> substitute_base("AAGTTAGTCA", "A", "C")
"CCGTTCGTCG"
Now imagine that we have a list of DNA sequences and we want to make a base substitution in all of them. Write the function substitute_sequences
that takes as input a list of strings representing DNA sequences, a base to be substituted (old), and a base that is to take the place of the base being substituted (new). This function should return a list of DNA sequences with the proper base substituted in each sequence (make sure to use list comprehensions or HOFs (filter
, map
) here):
>>> sequences = ["AAGTTAGTCA", "CTCGAGTCCGAAAGC", "AAGTTCCGACTG"]
>>> substitue_sequence(sequences, "A", "C")
["CCGTTCGTCC", "CTCGCGTCCGCCCGC", CCGTTCCGCTG"]
You can test these functions using the same command as in previous exercises, just replace the function number with either 3.1 or 3.2