Dictionaries are similar to lists, but instead of being indexed by integers they are indexed by keys.
grades = {'John Smith':75, 'Alice Brown':93, 'Walter White': 68}
print(grades)
Dictionary values can be accessed using their keys:
grades['Walter White']
grades['Walter White'] = 95
print(grades)
Assigning to a key which is not in a dictionary adds that key to the dictionary:
grades['Albert Einstein'] = 50
print(grades)
Getting the list of keys in the dictionary:
list(grades.keys())
Getting the list of values of a dictionary:
list(grades.values())
Converting dictionary into a nested list:
list(grades.items())
Use dict()
to convert a list of lists into a dictionary:
mylist = [['first_n', 'Walter'], ['last_n', 'White'], ['age', 51]]
mydict = dict(mylist)
print(mydict)
mydict['first_n']
Checking if a key is in a dictionary:
grades
'Alice Brown' in grades
'Larry Taylor' in grades
Iteration over a dictionary iterates over its keys:
for k in grades:
print(k)
for k in grades:
print('{:20} score: {}'.format(k, grades[k]))
Note: Dictionary keys must be non-mutable objects (strings, numbers).
final_grades = {['Walter', 'White']: 100}
final_grades = {('Walter', 'White'): 100}
final_grades[('Walter', 'White')]
final_grades = {50011234:'B-', 50112345:'A', 32601234:'A-'}
final_grades[50011234]
The sorted()
function can be used to sort a list:
mylist = ['this', 'that', 'hello', 'bye', 'road', 'ice']
s_mylist = sorted(mylist)
print(s_mylist)
mylist2 = [1, 0, -100, 2.3, -4.7]
s_mylist2 = sorted(mylist2)
print(s_mylist2)
Use the argument reverse
to revert the list:
rs_mylist2 = sorted(mylist2, reverse=True)
print(rs_mylist2)
mylist3 = ['hello', 'this', 3, 4, 0, 'bye']
sorted(mylist3)
In order to sort such lists as above we need to specify a key function that will be evaluated on list elements prior to comparing them:
sorted(mylist3, key=str)
str(100)
scores = [[8, 2, 3], [6, 3, 7], [1, 4, 9], [6, 1, 4]]
sorted(scores)
def total_score(s):
tot = s[0]*0.25 + s[1]*0.25 + s[2]*0.50
return tot
sorted(scores, key=total_score)
The numpy function np.random.randint(low, high, size)
generates an array of the given size
of random integers from the range low
to high
:
import numpy as np
numbers = np.random.randint(1, 10, 1000)
numbers
number_counts = {}
for n in numbers:
if n not in number_counts:
number_counts[n] = 1
else:
number_counts[n] += 1
print(number_counts)
import matplotlib.pyplot as plt
import matplotlib.cm as cm
pop = [1409, 1339, 324, 263]
countries = ['China', 'India', 'USA', 'Indonesia']
x = plt.bar(range(1, len(pop)+1), pop, width= .4,tick_label= countries, color=cm.autumn(.7))
x[1].set_color('r')
plt.xticks(fontsize=15, rotation='vertical')
plt.show()
semilogy
semilogx
and loglog
plots¶plt.figure(figsize=(20,5))
x = np.linspace(1, 5, 200)
plt.subplot(141)
plt.title('$y=x^3$', fontsize=20)
plt.plot(x, x**3)
plt.subplot(142)
plt.title('$y=2^x$', fontsize=20)
plt.plot(x, 2**x)
plt.subplot(143)
plt.title('$y=\log(x)$', fontsize=20)
plt.plot(x, np.log(x))
plt.subplot(144)
plt.title('$y=x^{0.5}$', fontsize=20)
plt.plot(x, x**0.5)
plt.show()
semilogy
¶plt.semilogy(x, y)
creates a plot with coordinates (x, log(y)). In this way plots of functions of the form $y=a\cdot b^x$ are becoming straight lines:
plt.figure(figsize=(20,5))
x = np.linspace(1, 5, 200)
plt.subplot(141)
plt.title('$y=x^3$', fontsize=20)
plt.semilogy(x, x**3)
plt.subplot(142)
plt.title('$y=2^x$', fontsize=20)
plt.semilogy(x, 2**x)
plt.subplot(143)
plt.title('$y=\log(x)$', fontsize=20)
plt.semilogy(x, np.log(x))
plt.subplot(144)
plt.title('$y=x^{0.5}$', fontsize=20)
plt.semilogy(x, x**0.5)
plt.show()
semilogx
¶plt.semilogx(x, y)
creates a plot with coordinates (log(x), y). In this way plots of functions of the form $y=a\cdot log(x)$ are becoming straight lines:
plt.figure(figsize=(20,5))
x = np.linspace(1, 5, 200)
plt.subplot(141)
plt.title('$y=x^3$', fontsize=20)
plt.semilogx(x, x**3)
plt.subplot(142)
plt.title('$y=2^x$', fontsize=20)
plt.semilogx(x, 2**x)
plt.subplot(143)
plt.title('$y=\log(x)$', fontsize=20)
plt.semilogx(x, np.log(x))
plt.subplot(144)
plt.title('$y=x^{0.5}$', fontsize=20)
plt.semilogx(x, x**0.5)
plt.show()
loglog
¶plt.loglog(x, y)
creates a plot with coordinates (log(x), log(y)). In this way plots of functions of the form $y=a\cdot x^b$ are becoming straight lines:
plt.figure(figsize=(20,5))
x = np.linspace(1, 5, 200)
plt.subplot(141)
plt.title('$y=x^3$', fontsize=20)
plt.loglog(x, x**3)
plt.subplot(142)
plt.title('$y=2^x$', fontsize=20)
plt.loglog(x, 2**x)
plt.subplot(143)
plt.title('$y=\log(x)$', fontsize=20)
plt.loglog(x, np.log(x))
plt.subplot(144)
plt.title('$y=x^{0.5}$', fontsize=20)
plt.loglog(x, x**0.5)
plt.show()
plt.figure(figsize=(5,5))
x = np.linspace(1, 5, 200)
plt.loglog(x, x**3, label='$y=x^3$')
plt.loglog(x, x**0.5, label='$y=x^{0.5}$')
plt.loglog(x, x**(-0.5), label='$y=x^{-0.5}$')
plt.legend()
plt.show()