Tutorial A5 – Solutions

1 Make a list of data objects, each of a different type you know. Print the elements of the list and their type to the screen by index.

[1]:
list_ = ["string", 1, 1.0, 1j, [], {},]

print(list_[0], type(list_[0]))
print(list_[1], type(list_[1]))
print(list_[2], type(list_[2]))
print(list_[3], type(list_[3]))
print(list_[4], type(list_[4]))
print(list_[5], type(list_[5]))
string <class 'str'>
1 <class 'int'>
1.0 <class 'float'>
1j <class 'complex'>
[] <class 'list'>
{} <class 'dict'>

2 Create a dictionary that represents a shopping list and store it in a variable: Use three things you need to buy as keys and add how much you want to buy of them as values. Than add another item to the existing dictionary. Print the dictionary keys to the screen.

[2]:
buy = {
    "apples": 6,
    "bread": 1,
    "onions": 2,
}

buy["noodles"] = 1

print(buy.keys())
dict_keys(['apples', 'bread', 'onions', 'noodles'])

Advanced 1 Take the points (0, 0), (0, 1), (1, 0) and (1, 1).

  • Store each point in a variable as a tuple. Make an empty list and append each point to the list.

[3]:
p1 = (0, 0)
p2 = (0, 1)
p3 = (1, 0)
p4 = (1, 1)

list_ = []
list_.append(p1)
list_.append(p2)
list_.append(p3)
list_.append(p4)

print(list_)
[(0, 0), (0, 1), (1, 0), (1, 1)]
  • Change the second point (0, 1) to (0, -1) by overwriting the variable in which it is stored. What happens to this point in the list?

[4]:
p2 = (0, -1)
print(p2)
print(list_)
(0, -1)
[(0, 0), (0, 1), (1, 0), (1, 1)]
  • Change the third point (1, 0) to (-1, 0) by indexing this point stored in the list. What happens to this point outside of the list?

[5]:
list_[2] = (-1, 0)
print(p3)
print(list_)
(1, 0)
[(0, 0), (0, 1), (-1, 0), (1, 1)]

Now store each of the initial points ((0, 0), (0, 1), (1, 0) and (1, 1)) in variables as lists and append them to a list.

[6]:
p1 = [0, 0]
p2 = [0, 1]
p3 = [1, 0]
p4 = [1, 1]

list_ = []
list_.append(p1)
list_.append(p2)
list_.append(p3)
list_.append(p4)

print(list_)
[[0, 0], [0, 1], [1, 0], [1, 1]]
  • Change the second point [0, 1] to [0, -1] by overwriting the y-component of the variable in which it is stored. What happens to this point in the list?

[7]:
p2[1] = -1
print(p2)
print(list_)
[0, -1]
[[0, 0], [0, -1], [1, 0], [1, 1]]
  • Change the third point [1, 0] to [-1, 0] by indexing this point stored in the list and overwriting the its x-component. What happens to this point outside of the list?

[8]:
list_[2][0] = -1
print(p3)
print(list_)
[-1, 0]
[[0, 0], [0, -1], [-1, 0], [1, 1]]

Advanced 2 Take the string "High Performance Computing" and - convert this into a list.

[9]:
string = "High Performance Computing"
list_ = list(string)
print(list_)
['H', 'i', 'g', 'h', ' ', 'P', 'e', 'r', 'f', 'o', 'r', 'm', 'a', 'n', 'c', 'e', ' ', 'C', 'o', 'm', 'p', 'u', 't', 'i', 'n', 'g']
  • Use the tuple tuple("abcdefghijklmnopqrstuvwxyz ") to map the letters in the string to their index in the alphabet. Tipp: Use the method tuple.index and the built-in function map. Map takes a function as the first argument and a sequence (e.g. a string as the second. The function is applied to each element of the sequence. Convert the return value of map() to a list. Use string.casefold() to map also upper case characters.

[10]:
mapping_tuple = tuple("abcdefghijklmnopqrstuvwxyz ")
map_ = map(mapping_tuple.index, string.casefold())
map_ = list(map_)
print(map_)
[7, 8, 6, 7, 26, 15, 4, 17, 5, 14, 17, 12, 0, 13, 2, 4, 26, 2, 14, 12, 15, 20, 19, 8, 13, 6]
  • Create a dictionary from the map and the list that represents this mapping (d = {"letter": mapped_index}). Tipp: Use zip().

[11]:
dict_ = dict(zip(list_,  list(map_)))
print(dict_)
{'H': 7, 'i': 8, 'g': 6, 'h': 7, ' ': 26, 'P': 15, 'e': 4, 'r': 17, 'f': 5, 'o': 14, 'm': 12, 'a': 0, 'n': 13, 'c': 2, 'C': 2, 'p': 15, 'u': 20, 't': 19}

Advanced 3 Consider the following dictionary of random floating values associated with random string keys:

[12]:
dictionary = {
    'afsgfabkpetv': 0.50996581835599997,
    'ajpjggwdnbbv': 0.48394650530200001,
    'bbsqfnzsaerh': 0.94967251027800004,
    'bmkkubxijsgoi': 0.73021130082789998,
    'bmmqvyintpoat': 0.47911570513349999,
    'ccgasifxzngp': 0.32569065293400001,
    'cebmdkidwwyt': 0.97307019405200001,
    'cismykmadevcya': 0.20194133057088001,
    'cjktphzhhbvrn': 0.27046062498669998,
    'ckdgztgvksmid': 0.3741114083232,
    'cwnukrruweshiuh': 0.53529824780703295,
    'cwthembmixxmg': 0.089824943543700006,
    'dasjwthqotarfgz': 0.48673599515272398,
    'dgoxsygqjkkiv': 0.49586035923279997,
    'dijwhwnubajohkf': 0.074657439369638007,
    'dszizjhbozuuy': 0.19034104761039999,
    'xcvhdthqotarfgk': 0.48673599515272398,
    'ehuzgmflwhxcnmv': 0.027337878930186001,
    }
  • Use the built-in properties of sets to find out whether any two values are the same. How could you check if a set already contains a dictionary value? Could two keys ever be the same?

[13]:
# Number of values in dict equal to unique values?
len(set(dictionary.values())) == len(dictionary.values())
[13]:
False
[14]:
# Value (dict[key]) in set?
dictionary['xcvhdthqotarfgk'] in set(dictionary.values())
[14]:
True

Keys in dictionary have to be unique by definition.

  • Compute the sum of all values in the dictionary.

[15]:
sum(dictionary.values())
[15]:
7.684977957563385
  • Make a set of unique letters found in all dictionary keys. Tipp: Use the string method str.join().

[16]:
set("".join(dictionary.keys()))
[16]:
{'a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'g',
 'h',
 'i',
 'j',
 'k',
 'l',
 'm',
 'n',
 'o',
 'p',
 'q',
 'r',
 's',
 't',
 'u',
 'v',
 'w',
 'x',
 'y',
 'z'}