ExercisesΒΆ
Create manually:
An empty dictionary. Check whether the dictionary is empty with
len()
.A dictionary
pronouns
representing these correspondences:1 -> "me" 2 -> "you" 3 -> "he" ...
Create the dictionary in 2 ways:
- In a single line of code.
- Starting from an empty dictionary and adding one by one single key value pairs.
A dictionary
tenfold_of
implementing the function \(f(n) = 10 n\), matching each key (an integer) with its tenfold.Keys should be integers from 1 to 5.
Once the dictionary is built, apply it to all the elements of
range(2, 5)
with a list comprehension and print to screen the result.Repeat the same for all the keys of
tenfold_of
.Hint: you can obtain the list of all keys with
keys()
.A dictionary
info_2TMV
codifying structured information about the PDB entry 2TMV containing the structure of a capsid protein of the Tobacco Mosaic Virus.- To the key
"pdb_id"
associate the value"2TMV"
. - To the key
"uniprot_id"
associate the value"P69687 (CAPSD_TMV)"
. - To the key
"number_scp_domains"
associate1
. - To the key
"number_pfam_domains"
associate1
.
- To the key
A dictionary
relatives_of
representing this network:JULIE ---- FRANCIS ---- MATTHEW | | + ----- BENEDICT -------+
as in the example we saw before.
Once the dictionary is built, print to screen the number of relatives of
"JULIE"
.A dictionary
from_2_bit_to_integer
representing the map from the following pairs of integers to integer:0, 0 -> 0 # 0*2**1 + 0*2**0 = 0 0, 1 -> 1 # 0*2**1 + 1*2**0 = 1 1, 0 -> 2 # 1*2**1 + 0*2**0 = 2 1, 1 -> 3 # 1*2**1 + 1*2**1 = 3 ^^^^ ^ ^^^^^^^^^^^^^^^^^^^ key value explanation
Once the dictionary is built, pront to screen the value associated with one of the keys (choose arbitrarily).
Given:
ratios = { ("A", "T"): 10.0 / 5.0, ("A", "C"): 10.0 / 7.0, ("A", "G"): 10.0 / 6.0, ("T", "C"): 5.0 / 7.0, ("T", "G"): 5.0 / 6.0, ("C", "G"): 7.0 / 6.0, }
representing ratios between the number of A, T, C, e G in a sequence:
What is the difference between
len(ratios)
,len(ratios.keys())
,len(ratios.values())
elen(ratios.items())
?Check whether
ratios
contains the key("T", "A")
. And the key("C", "G")
?Hint: can we use
keys()
? Can we use another method?Check whether it contains the value 2. And the value 3?
Hint: can we use
values()
?Check whether it contains the key-value pair
(("A", "T"), 2)
. And the key-value pair(("C", "G"), 1000)
?Hint: can we use
items()
?Use a list comprehension to extract the keys of the result of
items()
. Do the same to extract values.
Given:
map = { "zero": 1, "one": 2, "two": 4, "three": 8, "four": 16, "five": 32, }
Concatenate all the keys
map
, separated by spaces, in a single stringstring_of_keys
.Concatenate all the values of
map
as strings, separated by spaces, in a single stringstring_of_values
.Hint: note that the values of
map
are not strings!Insert in a list all the keys of
map
.Insert in a list all the keys of
map
, ordered alphanumerically.Hint: is the list returned by
keys()
ordered?Insert in a list all the values of
map
, ordered according to the order of the corresponding keys.Hint: how can I order a list based on the order of another list?
Given:
translation_of = {"a": "ade", "c": "cyt", "g": "gua", "t": "tym"}
translate the list:
list = ["A", "T", "T", "A", "G", "T", "C"]
into the string:
"ade tym tym ade gua tym cyt"
Hint: note that dictionary keys are in lower case, while the elements of
list
are in upper case! Start assuming they are not, then modify the code in order to account for this difference.