Part 1: Dictionaries

Part 1: Dictionaries

A dictionary is a collection of key-value pairs that allows access to the value by key (as opposed to access by index as in a list).

Let's look at some examples:

In [1]: # we can create an empty dictionary using braces codons = {} # we can add key value pairs using brackets codons['Met'] = 'AUG' codons['Tyr'] = 'UAU' # let's see what our dictionary looks like codons

Out[1]: {'Met': 'AUG', 'Tyr': 'UAU'}

Note the key, value relationship: dictionary_name[key] = value {key: value, key: value}

To get the value associated with a key we can use square brackets:

In [2]: codons['Met'] Out[2]: 'AUG'

You can create a dictionary by providing key-value pairs in the same format as the output from above:

In [4]: d = {'key1': 'value1', 'key2': 'value2'} d['key2']

Out[4]: 'value2'

The len function returns the number of key-value pairs:

In [5]: len(d) Out[5]: 2

We can change the values in a dictionary by referencing its key:

In [7]: d['key1'] = 'new_value' d

Out[7]: {'key1': 'new_value', 'key2': 'value2'}

Recall that we can do something similar with lists by referencing an index:

In [8]: l = [1,2,3] l[1] = 9 l

Out[8]: [1, 9, 3]

If we want to know if a dictionary contains a particular key we can use the in operator:

In [9]: 'key2' in d Out[9]: True

But using in to test if a dictionary contains a particular value doesn't work:

In [10]: 'value2' in d Out[10]: False

Just like lists and strings, dictionaries have useful methods. For example, we can use the values() method to determine if a dictionary has a particular value:

In [13]: 'value2' in d.values() Out[13]: True

Strings can be used as keys and integers can be also be used as keys:

In [15]: nums = {1: 'A', 2:'B', 3:'C'} nums

Out[15]: {1: 'A', 2: 'B', 3: 'C'}

Lists cannot be used as keys but they can be used as values:

In [20]: d_lists = {'list1': [1,2,3], 'list2': [4,5,6]} d_lists

Out[20]: {'list1': [1, 2, 3], 'list2': [4, 5, 6]}

You can iterate over a dictionary using a for loop:

In [21]: seqs = {'ATGAGA': 'seq1', 'AGACCC': 'seq2', 'GGGACA': 'seq3'} for key in seqs: print(key, seqs[key]) ATGAGA seq1 AGACCC seq2 GGGACA seq3

Note that in older version of Python, dictionaries are not ordered.

Dictionaries are extremely useful for efficiently keeping track of things. Let's write a function, kmers(seq, k) that computes the number of occurrences of all k-mers of length k in a sequence:

In [34]: def kmers(seq, k): d_kmers = {} for i in range(len(seq)-2): if seq[i:i+k] not in d_kmers: d_kmers[seq[i:i+k]] = 0 d_kmers[seq[i:i+k]] += 1 return d_kmers

kmers('ATGATGATG', 3) Out[34]: {'ATG': 3, 'TGA': 2, 'GAT': 2}

The get() method returns a value for a given key:

dict.get(key, default_value = None)

In [38]: codons = {'Ala': 'GCU', 'Met': 'AUG', 'Tyr': 'UAU'} codons.get('Ala') codons['Ala'] GCU GCU

But the get() methods is especially useful for assigning default values when creating new key-value pairs if they don't already exist:

In [40]: codons = {'Ala': 'GCU', 'Met': 'AUG', 'Tyr': 'UAU'} codons['Ala'] = codons.get('Ala', 'AAA') # value not changed codons['Pro'] = codons.get('Pro', 'CCA') # key and value added codons

Out[40]: {'Ala': 'GCU', 'Met': 'AUG', 'Tyr': 'UAU', 'Pro': 'CCA'}

Let's modify the kmer_count() function to use the get method:

In [44]: def kmers(seq, k): d_kmers = {} for i in range(len(seq)-2): d_kmers[seq[i:i+k]] = d_kmers.get(seq[i:i+k], 0) + 1 return d_kmers

kmers('ATGATGATG', 3) Out[44]: {'ATG': 3, 'TGA': 2, 'GAT': 2}

The second argument of the get method of a dictionary is the value which should be used in case the given key is not in the dictionary, which allowed us to skip the step that handles the case of a kmer that is not in the dictionary.

Part 2: Tuples

A tuple is a sequence of values much like a list. The values stored in a tuple can be any type, and they are indexed by integers. The important difference is that tuples are immutable, and hence can be used as keys for a dictionary.

A tuple can be created as a comma-separated list of values:

In [45]: t = 1,2,3 t

Out[45]: (1, 2, 3)

Although it is not necessary, it is common to enclose tuples in parentheses to help us identify tuples when we look at Python code:

In [47]: t = (1,2,3) t

Out[47]: (1, 2, 3)

To create a tuple with a single element, you have to include a final comma:

In [48]: t = 1, t

Out[48]: (1,)

What type of object is created if you omit the final comma? You can also create a tuple using the tuple keyword:

In [53]: t = tuple((1,2,3)) t

Out[53]: (1, 2, 3)

Similarly, you can create dictionaries using the dict keyword:

In [55]: d = dict({1:'A', 2:'B'}) d

Out[55]: {1: 'A', 2: 'B'}

And you can use the list keyword to creat a list:

In [60]: l = list([1,2,3])

You can convert a tuple to a list using the list() function:

In [61]: t = 1,2,3 l = list(t) l

Out[61]: [1, 2, 3]

And vice versa:

In [62]: t = tuple(l) t

Out[62]: (1, 2, 3)

Notice the delimiter around each type of object helps to define what type it is.

Tuples contain a set of elements just like lists and can be indexed exactly like lists:

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download