List Comprehension, Dictionaries &Benchmark Analysis
Prakash [email protected]://prakashgautam.com.np/6cs008
Agenda
➔ List Comprehension➔ Benchmark Analysis➔ Dictionaries
2
List Comprehension
➔ Explicitly write the whole thing
➔ Write a loop to create it
Squares = [0,1,4,9,16,25,36,49,64,81,100]
3
Squares = []for i in range(11)
Squares.append(i*i)
List Comprehension
➔ Write a loop to create it
➔ Write a List Comprehension
4
Squares = []for i in range(11)
Squares.append(i*i)
Squares = [i*i for i in range(11)]
List Comprehension
➔ Write a List Comprehension
➔ List Comprehension is a concise description of list➔ List Comprehension is a shorthand for a loop
5
Squares = [i*i for i in range(11)]
List Comprehension
6
[expression for item in list if conditional]
for item in list: if conditional:
expression
7
8
9
1. List of Squares
10
Squares = [i**2 for i in range(1,101)]print(Squares)
Squares = []for i in range(1,101):
Squares.append(i**2)print(Squares)
2. Remainders of 5
11
Remainder5 = [i**2%5 for i in range(1,10)]print(Remainder5)
P-Remainders = [i**2%p for i in range(0,p)
Quadratic Reciprocity [(p+1)/2]
Carl Friedrich Gauss
3. Movie that starts with ‘G’
12
gmovies = [title for title in movies if title.startswith(“G”)]print(gmovies)
movies = [a,b,c,....,n]gmovies = []for title in movies:
if title.startswith(“G”):gmovies.append(title)
print(gmovies)
4. List of tuples containing movie name & R year
13
gmovies = [title for(title,year) in movies if year<2000]print(gmovies)
movies = [(a,1941),(b,2000),....,(n,2017)]gmovies = []for (title,year) in movies:
if year<2000:gmovies.append(title)
print(gmovies)
5. Scalar Multiplication
14
v = [10, -2, 3]vem = [4*x for x in v]print(vem)
6. Cartesian Product
15
A = {1,3} & B = {x,y}A x B = {(1,x),(1,y),(3,x),(3,y)}
cp = [(a,b) for a in A for b in B]print(cp)
Rene Descartes
?
16
def double(x):return x*2
Xy = [double(x) for x in range(10]print(Xy)
?
17
print([x+y for x in [10,20,30] for y in [1,2,3]])
Num_list = [y for y in range(100) if y%2==0 if y%5==0]print(Num_list)
Ob = [“Even” if i%2==0 else “Odd” for i in range(10)]print(Ob)
List Comprehension
➔ An elegant way to define and create lists based on existing lists➔ More compact & faster than normal functions & loops for creating lists➔ Every LC can be converted into for loop but viceversa may not be true
18
Benchmark Analysis
➔ If comparisons done correctly: its called benchmarking➔ Lets analyze some sorting algorithms
19
20
# Integers 100 10,000 1,000,000
Insertion 0.57 6064
Selection 0.63 6293
Bubble 1.43 15628
Merge 0.25 48.3 7023
Quick 0.25 44.4 7153
Tim 0.02 3.7 690
Randomly Ordered Input (Time in Milliseconds)
21
# Integers 10,000 1,000,000
Insertion 8.8
Selection 6278
Bubble 1.6
Merge 45.5 6561
Quick 35.9 5044
Tim 0.2 20.8
Already Sorted Input (Time in Milliseconds)
22
# Integers 10,000 1,000,000
Insertion 12028
Selection 6882
Bubble 22338
Merge 48.2 6851
Quick 37.1 5285
Tim 0.2 20
Reverse Sorted Input (Time in Milliseconds)
23
# Integers 10,000 1,000,000
Insertion 6037
Selection 6270
Bubble 15496
Merge 48.4 6871
Quick 43.6 Failed
Tim 3.3 386
Random Ordered Input with many duplicates
Observations
➔ Python’s built-in Timsort is crazy fast in all tests➔ Bubble sort: extremely slow except already sorted➔ Insertion sort faster than selection sort except for reverse sorted input➔ Quicksort is generally faster than Mergesort
24
Timsort
25
def TimSort():for x in range(0, len(arr), RUN):
arr[x:x+RUN] = Insertion(arr[x:x+RUN])RUNinc = RUNWhile RUNinc < len(arr):
for x in range(0, len(arr), 2*RUNinc):arr[x:x+2*RUNinc]=merge(arr[x:x+RUNinc], arr[x+RUNinc:x+2*RUNinc])
RUNinc=RUNinc*2
Timsort
➔ Designed in 2002 by Tim Peters➔ Adaptive, Inplace & Stable➔ Default Sorting: JAVA, Android, Python➔ Time Complexity(Asymptotic Analysis)
◆ Best: O(n)◆ Worst: O(nlogn)
26
Tuples
Seemingly Similar to Lists
27
Tuples are immutable
Unlike lists we cannot change elements
28
Dictionaries
➔ List◆ A linear collection of values that stay in order
➔ Dictionary◆ A bag of values, each with its own label
29
Dictionaries
➔ It organizes linked information➔ Examples:
◆ Word & Definition◆ Name & Phone Number◆ Username & Password
➔ If you know the 1st entry, you can immediately get the 2nd one➔ The first item is key and second is value➔ Keys: Immutable➔ Lists can’t be keys, why?
30
Dictionaries
31
>>d={“id”:100, “code”: “A500ND10”, “language”: “English”, “location”: “(27.450745, 87.349823)}
Key Value
id 100
code A500ND10
language English
location 27.450745, 87.349823
Dictionaries
32
>>type(d)<class ‘dict’>
#Define dictionary using dict constructor>>d2 = dict(code = “A500ND10”, language: “English”)
#Adding new data in d2 (quotes are necessary)>>d2[“id”]=100>>print(d2)>>print(d2[‘language’])
Dictionaries
33
#Define dictionary using dict constructor>>d2 = dict(code = “A500ND10”, language: “English”)
#Adding new data in d2 (quotes are necessary)>>d2[“id”]=100>>print(d2) #It works>>print(d2[‘language’]) #It works>>print(d2[‘location’]) #Will it work?
Dictionaries
34
#Define dictionary using dict constructor>>d2 = dict(code = “A500ND10”, language: “English”)
>>print(d2[‘location’]) #Will it work?>>#Solution 1>>if ‘location’ in d2:
print(d2[‘location’]) else:
print(“The collection doesn’t have location details”)
Dictionaries
35
#Define dictionary using dict constructor>>d2 = dict(code = “A500ND10”, language: “English”)
>>print(d2[‘location’]) #Will it work?>>#Solution 2>>try:
print(d2[‘location’]) except keyError:
print(“The collection does not have location details”)
Dictionaries
36
#How many methods dict have?>>dir(d2)
#To see what the specific method does>>help(d2.get)
>>loc = d2.get(‘location’, None)>>print(loc)
#To Print all key-value pairs>>for key in d2.keys():
value = d2[key] print(key, “=” , value)
Dictionaries
37
#To Print all key-value pairs>>for key, value in d2.items(): print(key, “=” , value)
Defining Dictionaries
38
No duplicate keys
39
Old values gets overwritten instead…!
Access
40
We can access values by keys, but not the other way around
All keys, values or both
41
Use d.keys( ), d.items( ) and d.values( )
Loop over Dictionaries
42
Print all key-value pairs of Dictionary
Dictionaries are collection of key-value pairs
Example: the keys are all words in the English language, and their corresponding values are the meanings.
43
Dictionaries: Complexity Analysis
➔ O(1): Constant Time◆ Anytime program takes constant # instructions regardless of the input◆ Accessing any element in Dict or List◆ Appending an element in Dict or List◆ Pushing an element onto the front
➔ O(n): Linear Time◆ When the program takes time i.e directly proportional to the input size◆ Creating a List with n elements◆ Traversing
44
Dictionaries: Complexity Analysis
➔ O(n2): Quadratic Time◆ When n hits a value of 10,000: n2 gets pretty big (100,000,000). ◆ Doubly nested loop that I've gone over in class so many time is O(n2)
● for i in xrange(n): for j in xrange(i): pass➔ O(logn): Logarithmic Time
◆ Binary Search➔ O(nlogn): Poly-Logarithmic Time
◆ Timsort
45
THANK YOU
?
46