Data Structures Akshay Singh. Lists in python can contain any data type Declaring a list: a =...

19
Python Data Structures Akshay Singh

Transcript of Data Structures Akshay Singh. Lists in python can contain any data type Declaring a list: a =...

Page 1: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

PythonData Structures

Akshay Singh

Page 2: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Lists in python can contain any data type

Declaring a list:

a = [‘random’,’variable’, 1, 2]

List

Page 3: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

List.append(x) – adds “x” to the end of the list

List.extend(L) – adds the list “L” to the end of the list

List.insert(I,x) – adds the “I” item to position “x” in the list.

List.remove(x) – removes the first occurrence of “x” from the list.

List methods

Page 4: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

List.pop(I) – remove the element at the “I” position and return it. Removes last element if left blank.

List.index(x) – Returns the position of the first occurrence of “x”

List.count(x) – Returns the number of times “x” appears in the list.

List Methods

Page 5: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

List.sort() – sorts the list.

List.reverse() – reverses the arrangement of elements in the list.

List Methods

Page 6: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Last element in is the first element out. Use append and pop functions.

Lists as Stacks

Page 7: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

First element in the list is the first the leave. Use list.append(x) to add to the list and

list.popleft() to remove the first element.

Lists as Queues

Page 8: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Filter() – returns items from a sequence where the specified function is satisfied.

def f(x): return x % 2 != 0 and x % 3 != 0 filter(f, range(2, 25))

Output:[5, 7, 11, 13, 17, 19, 23]

Functional Programming Tools – filter()

Page 9: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Map() – uses the sequence as arguments for the function and returns the results.

def cube(x): return x*x*x map(cube, range(1, 11))

Output:[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

Functional Programming Tools – map()

Page 10: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Reduce() – Sends the first two elements of the sequence to the function, then the result and the third element and so on.

def add(x,y): return x+y reduce(add, range(1, 11))

Output:55

Functional Programming Tools – reduce()

Page 11: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Easy way to work with lists. Similar to loops.

vec = [2, 4, 6] [3*x for x in vec]

Output:[6, 12, 18]

List Comprehensions

Page 12: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

The del() statement can be used to remove an element from a list.

It does not return a value.

del a[0] : deletes first element in list “a” del a[2:4] : deletes third and fourth element del a[:] : deletes all elements in list “a”

Function – del()

Page 13: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Tuples are a standard sequence data type. Tuples are a number of values seperated by

commas. Tuples may be nested.

t = 12345, 54321, 'hello!' u = t, (1, 2, 3, 4, 5) ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))

Tuples

Page 14: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Collection of elements with no duplicates Can test for membership of elements

basket = [‘a', ‘o', 'a’, ‘p', ‘o', ‘b'] fruit = set(basket) fruit >>>set([‘o', ‘p', ‘a', ‘b']) 'orange' in fruit >>>True

Sets

Page 15: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Similar to hash tables. A set of key:value pairs.

tel = {'jack': 4098, 'sape': 4139} tel.keys() >>> ['jack‘,’sape’]

Dictionaries

Page 16: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Retrieves keys and values from a dictionary.

knights = {'gallahad': 'the pure', 'robin': 'the brave'}

for k, v in knights.iteritems(): print k, v >>>gallahad the pure robin the brave

Looping: iteritems()

Page 17: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

While looping over a sequence, provides the position of elements.

for i, v in enumerate(['tic', 'tac', 'toe']): print i, v >>>0 tic >>>1 tac >>>2 toe

Looping: enumerate()

Page 18: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Zip(list1, list2) : loops 2 or possibly more sequences at the same time.

Reversed(list) : loops over the list in a reverse order.

Sorted(list) : returns the list in a sorted order, however leaves the original list unsorted.

Looping (contd.)

Page 19: Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]

Questions?