python data structures jugal lodaya. lists list.append(x) list.extend(l) list.insert(i, x) ...

18
Python Data Structures Jugal Lodaya

Upload: hilary-hodge

Post on 11-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Python Data Structures

Jugal Lodaya

Page 2: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Lists

list.append(x)

list.extend(L)

list.insert(i, x)

list.remove(x)

list.pop([i])

list.clear()

list.index(x)

list.count(x)

list.sort()

list.reverse()

list.copy()

Page 3: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Lists (example)

>>> a.remove(333)

>>> a

[66.25, -1, 333, 1, 1234.5, 333]

>>> a.reverse()

>>> a

[333, 1234.5, 1, 333, -1, 66.25]

>>> a.sort()

>>> a

[-1, 1, 66.25, 333, 333, 1234.5]

>>> a = [66.25, 333, 333, 1, 1234.5]

>>> print(a.count(333), a.count(66.25), a.count('x'))

2 1 0

>>> a.insert(2, -1)

>>> a.append(333)

>>> a

[66.25, 333, -1, 333, 1, 1234.5, 333]

>>> a.index(333)

1

Page 4: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Lists as Stacks (last-in, first-out)

>>> stack = [3, 4, 5]>>> stack.append(6)>>> stack.append(7)>>> stack[3, 4, 5, 6, 7]>>> stack.pop()7

>>> stack[3, 4, 5, 6]>>> stack.pop()6>>> stack.pop()5>>> stack[3, 4]

Page 5: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Lists as Queues (first-in, first-out)

>>> from collections import deque

>>> queue = deque(["Eric", "John", "Michael"])

>>> queue.append("Terry") # Terry arrives

>>> queue.append("Graham") # Graham arrives

>>> queue.popleft() # The first to arrive now leaves

'Eric'

>>> queue.popleft() # The second to arrive now leaves

'John'

>>> queue # Remaining queue in order of arrival

deque(['Michael', 'Terry', 'Graham'])

Page 6: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Lists Comprehensions

List comprehensions provide a concise way to create lists. Common applications• make new lists where each element is the result

of some operations applied to each member of another sequence or iterable

• create a subsequence of those elements that satisfy a certain condition.

Page 7: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Lists Comprehensions

Page 8: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Nested List Comprehensions

Page 9: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

The del Statement

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]>>> del a[0]>>> a[1, 66.25, 333, 333, 1234.5]>>> del a[2:4]>>> a[1, 66.25, 1234.5]>>> del a[:]>>> a[]del can also be used to delete entire variables:

>>>>>> del a

Page 10: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Tuples and Sequences

Page 11: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Tuple packing and Sequence unpacking

The statement t = 12345, 54321, 'hello!' is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:

>>>>>> x, y, z = tThis is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side.

Page 12: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Sets

Page 13: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Dictionaries

• sometimes found in other languages as “associative memories” or “associative arrays”. • Indexed by keys instead of a range of value, which can be any immutable type• Tuples can be used as keys if they contain only strings, numbers, or tuples

• if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified

• unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}

• The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del.

• Storing with used keys causes replacement.

Page 14: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Dictionaries

Page 15: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Looping Techniques

Page 16: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

More on Conditions

• In, not in – check if value is in a sequence• a<b==c checks if a is less than b and if b equals c• not, and, and or (in order from highest to lowest priority)• and/or are “short-circuit” operators

• A and B and C --- if B is false, it doesn’t check C

>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'>>> non_null = string1 or string2 or string3>>> non_null'Trondheim'

Page 17: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

Comparing Sequences and Other Types

• Lexicographical ordering• First two items are compares, then second two, then third two, etc• First items can determine the order of the entire type

(1, 2, 3) < (1, 2, 4)[1, 2, 3] < [1, 2, 4]'ABC' < 'C' < 'Pascal' < 'Python'(1, 2, 3, 4) < (1, 2, 4)(1, 2) < (1, 2, -1)(1, 2, 3) == (1.0, 2.0, 3.0)(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)

Page 18: Python Data Structures Jugal Lodaya. Lists  list.append(x)  list.extend(L)  list.insert(i, x)  list.remove(x)  list.pop([i])  list.clear()  list.append(x)

presentation.end()