0% found this document useful (0 votes)
8 views

ICT582 Topic 06

Uploaded by

Hammad anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

ICT582 Topic 06

Uploaded by

Hammad anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Topic 6

Collections and Sequences


Last Topic

§ introduce new compound data types


• tuples
• lists
§ idea of mutability
§ idea of aliasing
§ idea of cloning

6.0001 LECTURE 5 2
Today

§ Collections
§ Sets
§ Dictionaries
§ Use of dictionaries to solve problems
§ Sequence
§ Common characteristics of sequences

3
6.0001 LECTURE 6
Collection

§ A collection is a group of related values or objects


stored together in a single container
§ You can count the number of items in the collection (using
the len function)
§ You can iterate over its members in a loop
§ You can test whether an item is in a collection (using the
in and not in operator)

§ In some collections, these items are ordered (string, list,


and tuple), and in others (set), items have no fixed order
§ Some collections can be changed (mutable). Other
collections cannot be changed once they are created.4
6.0001 LECTURE 6
Collections In Python

§ Python defined 4 built-in collection types:


§ Tuple: ordered, immutable, can have duplicates
§ List: ordered, mutable, can have duplicates
§ Set: un-ordered, mutable, no duplicates
§ Dictionary: ordered, mutable, no duplicates

§ String can also be considered as a collection type.


§ In Topic 4 and Topic 5, we have already seen strings,
tuples and lists.
§ In this topic, we will look at sets and dictionaries.
5
6.0001 LECTURE 6
Sets

§ Like in mathematics, a set is a collection of unique


items.
§ In Python, a set is a collection of items. These items do
not have a fixed order in the set.
§ Each item only appears in a set once, ie, there cannot
be more than one occurrence of the same item in a set
§ An item in a set cannot be changed, therefore, mutable
objects such as lists, sets and dictionaries cannot be
members of a set
§ However, you can add an new item into a set and you
6.0001 LECTURE 6 can also remove an item from the set. 6
Define a Set

§ A set literal is defined using curly brackets


fruit = {"apple","banana","mango","cherry"}

§ A set may contain values of different types


myset = {34,"Python",True,12.8}

§ It can even have tuples in a set (because a tuple is


immutable)
s = {"abc",(1,2,3)}

§ While the following definition would cause an error:


s = {"abc",[1,2,3]}
7
6.0001 LECTURE 6
Functions for Sets
§ Most functions applicable to tuples and lists are also
applicable to sets
print(len(myset)) # => 4
print(type(myset)) # => <class 'set'>
print(isinstance(myset, set)) # => True
print(sorted(fruit)) # to a new list
# => ['apple','banana','cherry','mango']
marks = {76, 90, 51, 82}
print(min(marks)) # => 51
print(max(marks)) # => 90
print(sum(marks)) # => 299
6.0001 LECTURE 6
8
Indexing for a set?

§ As the items in a set do not have fixed order, there is


no index for an individual item of the set.
§ Therefore, we cannot use index to access an item in a
set.
§ To see that the items in a set have no fixed order,
create a small program set_order.py (as below) and
run it several times. You will see the order of items are
not always in the same order in different runs:
# set_order.py
s = { "apple", "banana", "cherry"}
print(s)
9
6.0001 LECTURE 6
Indexing for a set?

$ py setOrder.py
{'banana', 'cherry', 'apple'}
$ py setOrder.py
{'cherry', 'banana', 'apple'}
$ py setOrder.py
{'banana', 'apple', 'cherry'}
$

1
6.0001 LECTURE 6 0
Add and Remove Items from Set

§ Set has add and remove methods:


s = {1, 2, 3}
s.add(4) # => {1,2,3,4}
s.remove(3) # => {1,2,4}

§ Example: divide Perth post codes into South of River


and North of River
def perthPostCode(s):
northSet=southSet = set() # empty set
for x in s:
if x >= 6100:
southSet.add(x)
else:
northSet.add(x)
return northSet, southSet

perth = {6008, 6170, 6009, 6051, 6150, 6109}


north, south = perthPostCode(perth)
print("north of river: ", north) # {6008, 6009, 6051}
print("south of river: ", south)
6.0001 LECTURE 6
# {6170, 6150, 6109} 1
1
Set Union and Intersection

§ The union of two sets is a new set containing members of


both sets
s1 = { 1, 2, 3, 'a', 'b'}
s2 = { 'a', 'b', 'c', 1, 2 }
s3 = s1 | s2. # s3 contains elements of s1 and s2
print(s3) # {1,2,3,'a','b','c'}
Same as
s3 = s1.union(s2)
§ The intersection of two sets is a new set containing
members common to both sets
s1 = { 1, 2, 3, 'a', 'b'}
s2 = { 'a', 'b', 'c', 1, 2 }
s3 = s1 & s2 # s3 contains elements common to both s1 and s2
print(s3) # {1,2,'a','b'}
Same as
s3
6.0001 LECTURE 6 = s1.intersection(s2) 1
2
Dictionaries

§ A dictionary in Python is a list of key:value pairs. It is


an extremely useful data structure. It attaches a
unique key to each value, allowing us to access a
value by its key.
§ A dictionary is also known as an associative array,
map (C++ and Java), hash (Perl), table (Lua), object
(JavaScript).
§ In Python, a dictionary is similar to a list: it is mutable
and the items in a dictionary is ordered (since Python
3.7). However, there cannot be duplicates in a
dictionary (no two elements have the same key).
1
6.0001 LECTURE 6 3
Define a Dictionary

§ A dictionary literal is defined using curly brackets,


similar to sets. However, each element must be a
key:value pair.
student = {
"name" : "John",
"major" : "CS",
"age" : 25
"mobile" : "0410342765"
}

§ Since curly brackets are used to define both sets and


dictionaries, the following definition can be confusing:
x = {}
Ø as it could be seen as either an empty set or empty dictionary.
Ø In Python, the above definition is reserved for empty dictionary, not
empty set. To define am empty set, use set constructor instead: 1
6.0001 LECTURE 6 4
x = set()
Comparison of List and Dictionary
A list A dictionary
0 "John" "name" "John"
1 "CS" "major" "CS"
2 25 "age" 25
3 "0410342765" "mobile" "0410342765"
… … … …

§ List: use the index to access a list element


Ø The index must be an integer starting from 0. The index only tells you the
position of the value in the list, not the nature of the value (eg, whether it
is a student's name or his age)

§ Dictionary: use the key to access a dictionary element


Ø The key reflects the nature of the value, eg, using the key name to access
the student's name and use the key age to access the student's 1age.
6.0001 LECTURE 6 5
Access a Dictionary

§ Use the key to access the value


print("Student name: " + student['name'])
print("Student age: " + student['age'])

§ As a dictionary is an object, it has many built-in


methods. Another way to access the value is by using
the get method
print("Student name: " + student.get('name'))
print("Student age: " + student.get('age'))

§ Obtain all keys


print(student.keys()) # ["name", "major", "age", "mobile"]

§ Obtain all values


print(student.values()) # ["John", "CS", 25, "0410342765"]
1
6.0001 LECTURE 6 6
Add and Remove Items

§ To add a new item (a key:value pair), use a new key


student['gender'] = 'Male' # 'gender" is the key
student['address'] = '20 South St, Murdoch'

§ Alternatively, one may use update method to add one


or more new items to a dictionary
x = {'gender':'Male', 'address':'20 South St, Murdoch'}
student.update(x)

§ To remove an item from the dictionary and return it


x = student.pop['address']
print('address is %s" %x) # '20 South St, Murdoch'

§ To pop off the last item from the dictionary


x = student.popitem() 1
6.0001 LECTURE 6 7
Delete Items in a Collection

§ To delete an item with a given key from the dictionary


del student['gender']

§ del is not a function or method. It is a statement. It


can be used to delete an item from a list as well
student = [ 'John', 'CS', 25 ]
del student[1] # remove 'CS' from the list

§ del can be used to delete an entire collection.


a = { 1, 2, 3 }
b = [ "abc", "xy" ]
c = ( 10.5, "foo", True )
d = { 'a' : 1, 'b' : 2 }
del a, b, c, d
1
6.0001 LECTURE 6 8
Delete Unneeded Objects

§ An object such as a string, a list, a dictionary, etc, can


take up a lot of system resources (memory space and
computer processing time). When an object is no
longer needed, you should delete it to save the system
resources.
x = 10
y = "Hello, world!"
del x, y

§ Once deleted, an object is completely removed from


your process (running program). Any attempt to use it
will generate an error.
§ However, you can always define a new variable using
the variable name that has been deleted.
1
6.0001 LECTURE 6 9
Loop through a Dictionary

§ You can loop through a dictionary using its keys,


values, and both keys and values
grades = { 'HD':4, 'D':10, 'C':23, 'P':34, 'N':10 }
numStudents = 0
for x in grades.values():
numStudents += x
print("Total number of students is ", numStudents)

# print the grade distributions


for x in grades.keys():
print("%s : %0.1f " %(x, grades[x]/numStudents * 100))

# alternatively
for k,v in grades.items():
2
6.0001 LECTURE 6 0
print("%s : %0.1f " %(k, v/numStudents * 100))
Dictionary Alias and Clone

§ In Topic 5, we discussed object aliasing and cloning


Ø An alias is like to give a nickname to a variable, both the
original variable and the new variable are in fact represent
the same object.
Ø While a clone of an object is a separate copy of the original
object. The original variable and the new variable represent
two different objects, albeit the new objects have the same
initial value as the original object.
john = { 'name':'John', 'age':25 }
david = john # david is an alias of john
david['name'] = 'David' # john's name is also changed to David.

§ To create a clone of a dictionary, use copy method


peter = john.copy() # peter is a clone of john
peter['name'] = 'Peter' # this change would not affect
6.0001 LECTURE 6
2
1
john
Sequence Types

§ In Python, a sequence is a type of collection that holds


an ordered sequence of values. Each value is
identified by an index.
§ We have already seen some of the sequence types
Ø String, such as "Python"
Ø Tuple, such as ("Apple", "Cherry", "Gooseberry")
Ø List, such as ["abc", 1, 3.1415, True]
Ø Range, is a sequence of numbers, such as range(1,10,2)
=> 1,3,5,7,9

2
6.0001 LECTURE 6 2
Common Characteristics of
Sequences
§ Sequence types share many common operations and
characteristics (with exceptions)
Ø Indexing
Ø Slicing
Ø Repetition, eg, "abc"*3 => "abcabcabc"
Ø Length: such as len([1,2,3,4,5]) => 5
Ø Membership: in operation and not in operator
Ø Iteration: as in the for loop
§ Note, some of sequences are mutable, such as lists,
others are immutable, such as strings, tuples and
ranges
2
6.0001 LECTURE 6 3
Sequence: indexing

§ Each member of a sequence is accessible using its


index. The index always starts from 0.
Ø String
s = "Python is fun!"
s[0] # 'P'
s.index("fun") # 10
Ø Tuple
t = ("Apple", "Cherry", "Gooseberry")
t[2] # "Gooseberry"
t.index("Cherry") # 1
Ø List
l = [1, "ICT582", 3.14, True]
l[1] # "ICT582"
l.index(3.14) # 2

2
6.0001 LECTURE 6 4
Sequence: indexing
Ø Range
r = range(10) # 0,1,2,3,4,5,6,7,8,9
r[2] # 2
r.index(4) # 4

r2 = range(1,10,2) # 1,3,5,7,9
r2[2] # 5
r2.index(5) # 2

2
6.0001 LECTURE 6 5
Sequence: slicing

§ The same sequence slicing operations apply to all


sequence types.
Ø String
s = "Python is fun!"
fun = s[10:] # 'fun!'
Ø Tuple
t = ("Apple", "Cherry", "Gooseberry")
t[::2] # ("Apple", "Gooseberry")
Ø List
l = [1, "ICT582", 3.14, True]
l[1::2] # ["ICT582", True]
Ø Range
r = range(10) # 0,1,2,3,4,5,6,7,8,9
list(r[::-1]) # [9,8,7,6,5,4,3,2,1,0]
2
6.0001 LECTURE 6 6
Sequence: concatenation

§ The same sequence slicing operations apply to all


sequence types (except ranges).
Ø String
s = "Python"
s + " is fun!" # 'Python is fun!'
Ø Tuple
t1 = ("Apple", "Cherry")
t2 = ("Orange", "Mango")
t1 + t2 #("Apple","Cherry","Orange","Mango")
Ø List
l1 = ["ICT582", "ICT580"
l2 = ["ICT375", ICT286"]
l1 + l2 # ["ICT582","ICT580","ICT375",ICT286"]
Ø Range
Concatenation operation doesn’t apply to
2
6.0001 LECTURE 6
ranges 7
Sequence: repetition

§ A sequence, except ranges, can be repeated many


times using operator *
Ø String
s = "Python!"
s*3 # '"Python!Python!Python!"
Ø Tuple
t = (1,2,3)
t*3 # (1,2,3,1,2,3,1,2,3)
Ø List
l = ['a','b','c']
l*2 # ['a','b','c','a','b','c']
Ø Range
Repetition operation doesn’t apply to ranges

2
6.0001 LECTURE 6 8
Sequence: length

§ The length function len applies to all sequences


Ø String
s = "Python!"
len(s) # 7
Ø Tuple
t = (1,2,3)
len(t) # 3
Ø List
l = ['a','b','c']
len(l) # 3
Ø Range
r = range(1,10,2) # 1,3,5,7,9
len(r) # 5

2
6.0001 LECTURE 6 9
Sequence: membership

§ Use membership operators in and not in to


determine whether a value is in the sequence or not
Ø String
s = "Python!"
'P' in s # True
"on" in s # True
Ø Tuple
t = (1,2,3)
2 not in t # False
Ø List
l = ['a','b','c']
'x' in l # False
Ø Range
r = range(1,10,2) # 1,3,5,7,9
3 in r # True 3
6.0001 LECTURE 6 0
Sequence: iterable

§ You can iterate over the elements in any sequence in


a for loop
for element in sequence:
statement
. . . . . .
statement

3
6.0001 LECTURE 6 1
Example: Word Frequency

def lyrics_to_frequencies(lyrics):
myDict = {}
for word in lyrics:
if word in myDict:
myDict[word] += 1
else:
myDict[word] = 1
return myDict

3
6.0001 LECTURE 6 2
Example: Word Frequency

def most_common_words(freqs):
values = freqs.values()
best = max(values)
words = []
for k in freqs:
if freqs[k] == best:
words.append(k)
return (words, best)

3
6.0001 LECTURE 6 3
Example: Word Frequency

def words_often(freqs, minTimes):


result = []
done = False
while not done:
temp = most_common_words(freqs)
if temp[1] >= minTimes:
result.append(temp)
for w in temp[0]:
del(freqs[w])
else:
done = True
return result

3
6.0001 LECTURE 6 4
Example: Word Frequency

beatles = '''
Love, love me do Love, love me do
You know I love you You know I love you
I'll always be true I'll always be true
So please, love me do So please, love me do
Whoa, love me do Whoa, love me do

Love, love me do Love, love me do


You know I love you You know I love you
I'll always be true I'll always be true
So please, love me do So please, love me do
Whoa, love me do Whoa, love me do
Yeah, love me do
Someone to love Whoa, oh, love me do
Somebody new '''
Someone to love
Someone like you

wordList = beatles.split()
wordFreq = lyrics_to_frequencies(wordList)
print(words_often(wordFreq, 5))
3
6.0001 LECTURE 6 5
Example: Word Frequency

hong$ py word_freq.py
[(['love'], 20), (['me', 'do'], 14), (['you', 'Whoa,'], 5)]
hong$

3
6.0001 LECTURE 6 6
References

§ Python data structures:


https://docs.python.org/3/tutorial/datastructures.html
§ The Python Tutorial: https://docs.python.org/3/tutorial/
§ W3School Python Tutorial
https://www.w3schools.com/python/
§ Python 3 documentation https://docs.python.org/3/

3
6.0001 LECTURE 6 7

You might also like