Transcript Containers

Python Crash Course
Containers
Bachelors
V1.0
dd 13-01-2015
Hour 6
Introduction to language - containers
Container data types in Python are types whose instances are capable of storing
other objects. Some of the fundamental built-in container objects include:
• lists – the most popular container data type in python; can store any number of
any objects.
• tuples – similar to list, yet once created are immutable,
• sets – can store only unique elements,
• bytes – immutable sequence of integers in the range 0 <= x < 256,
• bytearray – like bytes, but mutable,
• dictionary – also known as associative arrays. They contain mapping of keys into
value,
• str – string, a sequence of unicode characters,
• range – a sequence of numbers — more precisely a list containing arithmetic
progressions
• array.array – present in the array module. Similar to list, yet during the
construction it is restricted to holding a specific data type,
Containers – Mutable, Immutable,
Hashable
Containers in Python can be either mutable or immutable. The fact that a container object is
immutable doesn’t always mean that the objects it holds are also immutable (e.g. an immutable tuple
holding mutable lists). However, container objects are fully immutable only if the object itself, and the
objects it contains are recursively immutable. Recursively immutable objects may be hashable. This
is important as only hashable objects can be used in a mapping container object (see below) as
keys.
Mutable Definition:
Mutable objects can change their value but keep their id().
Immutable Definition:
An object with a fixed value. Immutable objects include numbers, strings and
tuples. Such an object cannot be altered. A new object has to be created if
a different value has to be stored. They play an important role in places where
a constant hash value is needed, for example as a key in a dictionary.
Hashable Definition:
An object is hashable if it has a hash value which never changes during
its lifetime (it needs a hash()method), and can be compared to other objects
(it needs an eq() method). Hashable objects which compare equal must have
the same hash value.
Containers – Mutable, Immutable,
Hashable
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or
dictionaries) are.
Examples of mutable containers include:
• list,
• set,
• dictionary,
• bytearray
• array
Examples of immutable containers include:
• numbers
• string,
• frozenset,
• tuple,
• bytes
The main implication of the mutable/immutable distinction and hashability is that not all container
objects can store all other container objects, in particular:
• sets can store only hashable object (each object in set has to have a unique hash — sets do not
store duplicate objects, as opposed to e.g. lists or tuples)
• dictionaries can have only hashable objects as keys
Containers – Ordered or Unordered
Container object can store their content in either an ordered or unordered manner. Order, or lack of
thereof, is unrelated to the mutability of objects. This means that both mutable and immutable
objects can be either ordered or unordered.
Examples of ordered containers include:
• list,
• string,
• tuple,
• bytes,
• bytearrays,
• array
Examples of unordered containers include:
• dictionary,
• set,
• frozenset.
Introduction to language - containers
Data type
Mutable Ordered
Literal example
Constructor
Sequence types
list
yes
yes
[1,2,3]
list()
tuple
no
yes
(1,2,3)
tuple()
str
no
yes
“text” / ‘text’
range
no
yes
–
range()
bytes
no
yes
b’abcde’ / b”abc”
bytes()
bytearray
yes
yes
–
bytearray()
array *
yes
yes
–
array.array()
set
yes
no
{1,2,3}
frozenset
no
no
–
yes
no
{“key1″: “val”, “key2″: “val”}
str()
Set types
set()
frozenset()
Mapping types
dict
dict()
Containers - Lists
Lists are the most versatile of Python's compound data types. A list contains items separated by commas and
enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is
that all the items belonging to a list can be of different data type. The values stored in a list can be accessed using
the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end-1.
The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator.
Lists:
>>> a = [1, 2, 4, 8, 16] # list of ints
>>> c = [4, 'candles', 4.0, 'handles'] # can mix types
>>> c[1]
'candles'
>>> c[2] = 'knife'
>>> c[-1] # negative indices count from end
'handles'
>>> c[1:3] # slicing
['candles', 'knife']
>>> c[2:] # omitting defaults to start or end
['knife', 'handles']
>>> c[0:4:2] # variable stride (could just write c[::2])
[4, 'knife']
>>> a + c # concatenate
[1, 2, 4, 8, 16, 4, 'candles', 'knife', 'handles']
>>> len(a)
5
Lists Methods
SN Function with Description
SN
Methods with Description
1
list.append(obj)
Appends object obj to list
2
list.count(obj)
Returns count of how many times obj
occurs in list
1
cmp(list1, list2)
Compares elements of both lists.
2
len(list)
Gives the total length of the list.
3
max(list)
Returns item from the list with max index.
3
list.extend(seq)
Appends the contents of seq to list
4
min(list)
Returns item from the list with min index.
4
5
list(seq)
Converts a tuple into list.
list.index(obj)
Returns the lowest index in list that obj
appears
5
list.insert(index, obj)
Inserts object obj into list at offset index
6
list.pop(obj=list[-1])
Removes and returns last object or obj
from list
7
list.remove(value)
Removes object obj with value from list
8
list.reverse()
Reverses objects of list in place
9
list.sort([func])
Sorts objects of list, use compare func if
given
List operations
list1 = ['physics', 'chemistry', 1997, 2000]
for item in L:
print list1
print item
del list1[2]
print "After deleting value at index 2 : "
for index, item in enumerate(L):
print list1
print index, item
L = []
#empty list
i = iter(L)
L = list()
item = i.next() # fetch first value
item = i.next() # fetch second value
A = B = [] # both names will point to the same list
A = []
stack = []
B = A # both names will point to the same list
stack.append(object) # push
A = []; B = [] # independent lists
object = stack.pop() # pop from end
queue = []
queue.append(object) # push
object = queue.pop(0) # pop from beginning
List Comprehension
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each
element is the result of some operations applied to each member of another sequence or iterable, or to create a
subsequence of those elements that satisfy a certain condition.
>>>
>>>
...
...
>>>
[0,
squares = []
for x in range(10):
squares.append(x**2)
squares
1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
Nested list comprehension
>>> matrix = [
...
[1, 2, 3, 4],
...
[5, 6, 7, 8],
...
[9, 10, 11, 12],
... ]
To transpose this matrix:
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
Containers - Tuples
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by
commas. Unlike lists, however, tuples are enclosed within parentheses. The main differences between lists and
tuples are: Lists are enclosed in brackets ( [ ] ), and their elements and size can be changed, while tuples are
enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
Tuples:
>>> q = (1, 2, 4, 8, 16) # tuple of ints
>>> r = (4, 'candles', 4.0, 'handles') # can mix types
>>> s = ('lonely',) # singleton
>>> t = () # empty
>>> r[1]
'candles'
>>> r[2] = 'knife' # cannot change tuples
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> u = 3, 2, 1
# parentheses not necessary
>>> v, w = 'this', 'that'
>>> v
'this'
>>> w
'that'
>>>
>>>
(3,
>>>
>>>
>>>
(4,
>>>
u = 3, 2,1
print u
2, 1)
v= 4,6,7
w=v+u
print w
6, 7, 3, 2, 1)
Use of tuples
def func(x,y):
# code to compute x and y
return (x,y)
(x,y) = func(1,2)
Python Expression
Results
Description
len((1, 2, 3))
3
Length
(1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
Concatenation
('Hi!',) * 4
('Hi!', 'Hi!', 'Hi!', 'Hi!')
Repetition
3 in (1, 2, 3)
True
Membership
for x in (1, 2, 3): print x,
123
Iteration
Tuple methods
SN
Function with Description
1
cmp(tuple1, tuple2)
Compares elements of both tuples.
2
len(tuple)
Gives the total length of the tuple.
3
max(tuple)
Returns item from the tuple with max value.
4
min(tuple)
Returns item from the tuple with min value.
5
tuple(seq)
Converts a list into tuple.
Note: Tuples have no append or extend methods, nor can you remove items as there are no remove or pop
methods either.
Containers - Sets
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating
duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric
difference.
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> fruit = set(basket)
# create a set without duplicates
>>> fruit
set(['orange', 'pear', 'apple', 'banana'])
>>> 'orange' in fruit
# fast membership testing
True
>>> 'crabgrass' in fruit
False
>>> x = set("A Python Tutorial")
>>> x
set(['A', ' ', 'i', 'h', 'l', 'o', 'n', 'P', 'r', 'u', 't', 'a', 'y', 'T'])
>>> type(x)
<type 'set'>
>>>
Containers - Sets
No mutable objects
>>> cities = set((["Python","Perl"], ["Paris", "Berlin", "London"]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>>
But sets are mutable
>>> cities = set(["Frankfurt", "Basel","Freiburg"])
>>> cities.add("Strasbourg")
>>> cities
set(['Freiburg', 'Basel', 'Frankfurt', 'Strasbourg'])
>>>
frozensets are immutable
>>> cities = frozenset(["Frankfurt", "Basel","Freiburg"])
>>> cities.add("Strasbourg")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
>>>
Sets - operations
No mutable objects
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a
# unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> a - b
# letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b
# letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b
# letters in both a and b
set(['a', 'c'])
>>> a ^ b
# letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
frozensets are immutable
>>> cities = frozenset(["Frankfurt", "Basel","Freiburg"])
>>> cities.add("Strasbourg")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
>>>
Sets - operations
SN
Methods with Description
SN
Methods with Description
1
set.add(obj)
Adds object obj to set
8
2
set.clear(obj)
removes all objects from a set
3
set.copy ()
Creates a shallow copy of a set
set.intersection(set)
Returns the intersection of the instance
set and the set s as a new set. In other
words: A set with all the elements which
are contained in both sets is returned.
9
4
set.difference(set)
Returns the difference of two or more
sets as a new set.
set.isdisjoint(ser)
This method returns True if two sets have
a null intersection.
10
set.difference_uodate(set)
Removes all elements of another set from
this set. x.difference_update() is the
same as "x = x - y"
set.issubset(set)
x.issubset(y) returns True, if x is a subset
of y. "<=" is an abbreviation for "Subset
of"
11
set.issuperset(obj)
x.issuperset(y) returns True, if x is a
superset of y. ">=" is an abbreviation for
"issuperset of"
12
set.pop()
pop() removes and returns an arbitrary
set element. The method raises a
KeyError if the set is empty
5
6
set.discard(obj)
Removes obj from the set
7
set.remove(value)
Works like discard(), but if el is not a
member of the set, a KeyError will be
raised.
Containers - Dictionaries
Python 's dictionaries are hash table type. They work like associative arrays or hashes found in Perl and consist of
key-value pairs. Keys can be almost any Python type, but are usually numbers or strings. Values, on the other hand,
can be any arbitrary Python object. Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and
accessed using square braces ( [] ).
Dictionaries:
>>> a = {'eyecolour': 'blue', 'height': 152.0,
42: 'the answer'}
>>> a['age'] = 28
>>> a
{42: 'the answer', 'age': 28, 'eyecolour': 'blue', 'height': 152.0}
>>> del(a['height'])
>>> a
{42: 'the answer', 'age': 28, 'eyecolour': 'blue'}
>>> b = {}
>>> b['hello'] = 'Hi! '
>>> a.keys()
[42, 'age’, 'eyecolour’]
>>> a.values()
['the answer', 28, 'blue']
Dictionary use
>>> colors = { "blue": (0x30,0x30,0xff), "green": (0x30,0xff,0x97),
... "red": (0xff,0x30,0x97), "yellow": (0xff,0xff,0x30) }
>>> for c in colors:
...
print c, colors[c]
en_ne = {"red" : "rood", "green" : "groen", "blue" : "blauw", "yellow":"geel"}
print en_ne["red"]
ne_fr = {"rood" : "rouge", "groen" : "vert", "blauw" : "bleu", "geel":"jaune"}
print "The French word for red is: " + ne_fr[en_ne["red"]]
>>> dic = { [1,2,3]:"abc"}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list objects are unhashable
>>> dic = { (1,2,3):"abc", 3.1415:"abc"}
>>> dic
{(1, 2, 3): 'abc'}
dictionaries = {"en_ne" : en_ne, "ne_fr" : ne_fr }
print dictionaries["ne_fr"]["blauw"]
Dictionary methods
SN Function with Description
SN Methods with Description
1
cmp(dict1, dict2)
Compares elements of both dict.
1
dict.clear()
Removes all elements of dictionary dict
2
len(dict)
Gives the total length of the
dictionary. This would be equal to
the number of items in the
dictionary.
2
dict.copy()
Returns a shallow copy of dictionary dict
2
dict.fromkeys()
Create a new dictionary with keys from seq and
values set to value.
3
dict.get(key, default=None)
For key key, returns value or default if key not in
dictionary
4
dict.has_key(key)
Returns true if key in dictionary dict, false otherwise
5
dict.items()
Returns a list of dict's (key, value) tuple pairs
6
dict.keys()
Returns list of dictionary dict's keys
7
dict.setdefault(key, default=None)
Similar to get(), but will set dict[key]=default
if key is not already in dict
8
dict.update(dict2)
Adds dictionary dict2's key-values pairs to dict
9
dict.values()
Returns list of dictionary dict2's values
3
str(dict)
Produces a printable string
representation of a dictionary
4
type(variable)
Returns the type of the passed
variable. If passed variable is
dictionary then it would return a
dictionary type.
Dictionary operations
for key, value in someDictionary.items():
# process key and value
for value in someDictionary.values():
# process the value
>>> i = { "two":2, "three":3, "quatro":4 }
>>> del i["quatro"]
>>> i
{'two': 2, 'three': 3}
>>> i = { "two":2, "three":3, "quatro":4 }
>>> i.pop("quatro")
4
>>> i
{'two': 2, 'three': 3}
# zipping two lists into a dictionary
>>> dishes = ["pizza", "sauerkraut", "paella", "Hamburger"]
>>> countries = ["Italy", "Germany", "Spain", "USA"]
>>> country_specialities_dict = dict(country_specialities)
>>> print country_specialities_dict
{'Germany': 'sauerkraut', 'Spain': 'paella', 'Italy': 'pizza', 'USA': 'Hamburger'}
Type Conversions
Function
int(x [,base])
Description
Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )
float(x)
Converts x to a long integer. base specifies the base if x is a string.
Converts x to a floating-point number.
complex(real [,imag])
str(x)
Creates a complex number.
Converts object x to a string representation.
repr(x)
eval(str)
Converts object x to an expression string.
Evaluates a string and returns an object.
tuple(s)
list(s)
set(s)
dict(d)
Converts s to a tuple.
Converts s to a list.
Converts s to a set.
Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s)
chr(x)
unichr(x)
Converts s to a frozen set.
Converts an integer to a character.
Converts an integer to a Unicode character.
ord(x)
Converts a single character to its integer value.
hex(x)
oct(x)
Converts an integer to a hexadecimal string.
Converts an integer to an octal string.
Shallow and Deep copy
Python creates real copies only if it has to, i.e. if the user, the programmer, explicitly demands it.
# Expected behaviour
>> colours1 = ["red", "green"]
>>> colours2 = colours1
>>> colours2 = ["rouge", "vert"]
>>> print colours1
['red', 'green']
# Watch OUT!
>>> colours1 = ["red", "green"]
>>> colours2 = colours1
>>> colours2[1] = "blue"
>>> colours1
['red', 'blue']
# Force to copy
>>> list1 = ['a','b','c','d']
>>> list2 = list1[:]
>>> list2[1] = 'x'
>>> print list2
['a', 'x', 'c', 'd']
>>> print list1
['a', 'b', 'c', 'd']
>>>
# But be carefull with sublists!
>>> lst1 = ['a','b',['ab','ba']]
>>> lst2 = lst1[:]
>>> lst2[0] = 'c'
>>> lst2[2][1] = 'd'
>>> print(lst1)
['a', 'b', ['ab', 'd']]
Shallow and Deep copy
Special module copy allows for deepcopy of the whole object
>>> from copy import deepcopy
>>>
>>> lst1 = ['a','b',['ab','ba']]
>>>
>>> lst2 = deepcopy(lst1)
>>>
>>> lst2[2][1] = "d"
>>> lst2[0] = "c";
>>>
>>> print lst2
['c', 'b', ['ab', 'd']]
>>> print lst1
['a', 'b', ['ab', 'ba']]
>>>
Logical Operators
End