Python: Dictionaries (Solutions)ΒΆ

  1. Solutions:

    1. Solution:

      empty_dict = {}
      print empty_dict
      print len(empty_dict)                        # 0
      
    2. Solution:

      pronouns = {}
      pronouns[1] = "I"
      pronouns[2] = "you"
      pronouns[3] = "he"
      pronouns[4] = "we"
      pronouns[5] = "you"
      pronouns[6] = "they"
      

      or:

      pronouns = {
          1: "I", 2: "you", 3: "he",
          4: "we", 5: "you", 6: "them",
      }
      
    3. Solution:

      tenfold_of = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50}
      
      print [tenfold_of[n] for n in range(2, 5)]
      
      print [tenfold_of[key] for key in tenfold_of.keys()]
      
    4. Solution:

      info_2TMV = {
          "pdb_id": "2TMV",
          "uniprot_id": "P69687 (CAPSD_TMV)",
          "number_scp_domains": 1,
          "number_pfam_domains": 1,
      }
      
    5. Solution:

      relatives_of = {
          "JULIE": ["FRANCIS", "BENEDICT"],
          "FRANCIS": ["JULIE", "MATTHEW"],
          "MATTHEW": ["FRANCIS", "BENEDICT"],
          "BENEDICT": ["JULIE", "MATTHEW"],
      }
      
      num_relatives_of_julie = len(relatives_of["JULIE"])
      print num_relatives_of_julie
      
    6. Solution:

      from_2_bit_to_integer = {
          (0, 0): 0,
          (0, 1): 1,
          (1, 0): 2,
          (1, 1): 3,
      }
      

      Note that we cannot use lists as keys: lists are not immutable!

      Let’s print the value corresponding to 1, 0:

      print from_2_bit_to_integer[(1, 0)]
      #                           ^^^^^^
      #                            tuple
      
  2. Solution:

    ratios = {
        ("A", "T"): 10.0 / 3.0,
        ("A", "C"): 10.0 / 7.0,
        ("A", "G"): 10.0 / 6.0,
        ("T", "C"): 3.0 / 7.0,
        ("T", "G"): 3.0 / 6.0,
        ("C", "G"): 7.0 / 6.0,
    }
    
    
    print len(ratios)                             # 6
    print len(ratios.keys())                      # 6
    print len(ratios.values())                    # 6
    print len(ratios.items())                     # 6
    # they all count the number of key-value pairs!
    
    
    # let's pront the keys of the dictionary, to get an idea
    print ratios.keys()                           # a list of tuples!
    print type(ratios.keys())                     # list
    print type(ratios.keys()[0])                  # tuple
    
    contains_T_A = ("T", "A") in ratios.keys()
    print contains_T_A                              # False
    
    contains_C_G = ("C", "G") in ratios.keys()
    print contains_C_G                              # True
    
    # let's do the same with has_key()
    print ratios.has_key(("T", "A"))              # False
    print ratios.has_key(("C", "G"))              # True
    
    
    # let's print the values of the dictionary to get an idea
    print ratios.values()                         # a list of int!
    print type(ratios.values()[0])                # int
    
    contains_2 = 2 in ratios.values()
    print contains_2                                # True
    
    contains_3 = 3 in ratios.values()
    print contains_3                                # False
    
    
    # let's print the key-value pairs to get an idea
    print ratios.items()
    # it's a list of pairs (tuple): the first element, the key, is
    # a tuple itself, the second is an int
    
    print (("A", "T"), 2) in ratios.items()       # True
    print (("C", "G"), 1000) in ratios.items()     # False
    
    
    # list comprehension:
    keys = [key_value[0]
              for key_value in ratios.items()]
    values = [key_value[-1]
              for key_value in ratios.items()]
    
  3. Solution:

    map = {
        "zero": 1,
        "one": 2,
        "two": 4,
        "three": 8,
        "four": 16,
        "five": 32,
    }
    
    
    # all the keys of map are strings so keys() returns a list of strings
    # we can use directly  join()
    string_of_keys = " ".join(map.keys())
    
    
    # the values of map are int, so we cannot use directly join()
    # we first have to transform all values from int to string
    string_of_values = " ".join(str(value) for value in map.values())
    list_of_keys = map.keys()
    print list_of_keys                       # not ordered
    
    
    ordered_list_of_keys = map.keys()
    ordered_list_of_keys.sort()
    print ordered_list_of_keys               # now it is ordered
    
    
    list_of_values_ordered_by_keys = \
        [map[key] for key in ordered_list_of_keys]
    
  4. Solution:

    # using a list comprehension we can apply the dictionary to
    # the list: it's necessary to use lower() *before*
    # using an aminoacid as a key of translation_of!
    translation= [translation_of[aa.lower()] for aa in list]
    print translation
    
    # now we can use join() to concatenate the various parts
    result = " ".join(translation)
    print result
    

    or, in a single line:

    print " ".join([translation_of[aa.lower()] for aa in list])
    
  5. Solution:

    propensities = {
        'N': 0.2299, 'P': 0.5523, 'Q': -0.1877,
        'A': -0.2615, 'R': -0.1766, 'S': 0.1429,
        'C': -0.01515, 'T': 0.0089, 'D': 0.2276,
        'E': -0.2047, 'V': -0.3862, 'F': -0.2256,
        'W': -0.2434, 'G': 0.4332, 'H': -0.0012,
        'Y': -0.2075, 'I': -0.4222, 'K': -0.100092,
        'L': 0.33793, 'M': -0.22590
    }
    
    # using a list comprehension
    pos_aa = [aa for aa in propensities.keys() if propensities[aa]>0]
    print pos_aa