[go: up one dir, main page]

0% found this document useful (0 votes)
34 views59 pages

HKU - 7001 - 2. More On Python

Uploaded by

lo
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)
34 views59 pages

HKU - 7001 - 2. More On Python

Uploaded by

lo
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/ 59

More on Python

MSBA7001 Business Intelligence and Analytics


HKU Business School
The University of Hong Kong

Instructor: Dr. DING Chao


Agenda
• Lists
• Dictionaries
• Tuples
Lists
A List is a sequence
• Like a string, a list is a sequence of values. In a string, the
values are characters
• In a list, they can be any type. The values in list are called
elements or sometimes items
• The simplest way to create a list is to enclose the elements
in square brackets “[" and “]”, the elements in the list are
separated by commas
• We can also define a list by calling the list() function

friends = ['Joseph', 'Glenn', 'Sally']


numbers = [10, 20, 30, 40]
names = list() # it defines an empty list
A List is a sequence
• A list element can be any Python object - even another list
• A list can be empty

>>> print([1, 24, 76])


[1, 24, 76]
>>> print(['red', 'yellow', 'blue'])
['red', 'yellow', 'blue']
>>> print(['red', 24, 98.6])
['red', 24, 98.6]
>>> print([ 1, [5, 6], 7])
[1, [5, 6], 7]
>>> print([])
[]
A List is a sequence
• We have already used list in loops

friends = ['Joseph', 'Glenn', 'Sally']


for friend in friends:
print('Happy New Year:', friend)
print('Done!')

Happy New Year: Joseph


Happy New Year: Glenn
Happy New Year: Sally
Done!
A List is a sequence
• Just like strings, we can get at any single element in a list
using an index specified in square brackets

Joseph Glenn Sally


0 1 2

>>> friends = ['Joseph', 'Glenn', 'Sally']


>>> print(friends[1])
Glenn
>>>
Lists are Mutable
• Strings are “immutable” - we cannot change the contents of
a string - we must make a new string to make any change
• Lists are “mutable” - we can change an element of a list
using the index operator

>>> fruit = 'Banana' >>> lotto = [2, 14, 26, 41, 63]
>>> fruit[0] = 'b' >>> print(lotto)
Traceback [2, 14, 26, 41, 63]
TypeError: 'str' object does not >>> lotto[2] = 28
support item assignment >>> print(lotto)
>>> x = fruit.lower() [2, 14, 28, 41, 63]
>>> print(x)
banana
Length of a list
• The len() function takes a list as a parameter and returns
the number of elements in the list
• Actually len() tells us the number of elements of any set
or sequence (such as a string...)

>>> greet = 'Hello Bob'


>>> print(len(greet))
9
>>> x = [1, 2, 'joe', 99]
>>> print(len(x))
4
Traversing a list

friends = ['Joseph', 'Glenn', 'Sally']


for friend in friends:
print('Happy New Year:', friend)

for i in range(len(friends)):
friend = friends[i]
print('Happy New Year:', friend)

Happy New Year: Joseph


Happy New Year: Glenn
Happy New Year: Sally
Concatenating Lists Using +
• We can create a new list by adding two existing lists
together

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print(c)
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]
List slicing
• List can be sliced just like strings, but remember, the second
number is “up to but not including”

>>> t = [9, 41, 12, 3, 74, 15]


>>> t[1:3]
[41,12]
>>> t[:4]
[9, 41, 12, 3]
>>> t[3:]
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
List Methods
• Python provides methods that operate on lists. To see these
methods

>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
['append', 'count', 'extend', 'index',
'insert', 'pop', 'remove', 'reverse', 'sort']
Building a List from Scratch
• We can create an empty list and then add elements using
the append method, or extend method.
• The list stays in order and new elements are added at the
end of the list

>>> stuff = list() >>> list_A = [1,2,3]


>>> stuff.append('book') >>> list_B = [4,5,6]
>>> stuff.append(99) >>> list_A.extend(list_B)
>>> print(stuff) >>> print(list_A)
['book', 99] [1,2,3,4,5,6]
>>> stuff.append('cookie')
>>> print(stuff)
['book', 99, 'cookie']
Deleting an element from a list
• There are several ways to delete elements from a list. If you
know the index of the element you want, you can use
pop() or del or remove():

>>> t = ['a', 'b', 'c'] >>> t = ['a', 'b', 'c']


>>> x = t.pop(1) >>> del t[1]
>>> print(t) >>> print(t)
['a', 'c'] ['a', 'c']
>>> print(x)
b
>>> t = ['a', 'b', 'c']
>>> t.remove('b')
>>> print(t)
['a', 'c']
Is Something in a List?
• Python provides two operators that let you check if an item
is in a list
• These are logical operators that return True or False
• They do not modify the list

>>> some = [1, 9, 21, 10, 16]


>>> 9 in some
True
>>> 15 in some
False
>>> 20 not in some
True
Lists are in Order
• A list can hold many items and keeps those items in the
order until we do something to change the order
• A list can be sorted (i.e., change its order)
• The sort() method means “sort yourself”

>>> friends = [ 'Joseph', 'Glenn', 'Sally' ]


>>> friends.sort()
>>> print(friends)
['Glenn', 'Joseph', 'Sally']
>>> print(friends[1])
Joseph
>>>
Built-in Functions and Lists
• There are a number of functions built into Python that take
lists as parameters

>>> nums = [3, 41, 12, 9, 74, 15]


>>> print(len(nums))
6
>>> print(max(nums))
74
>>> print(min(nums))
3
>>> print(sum(nums))
154
>>> print(sum(nums)/len(nums))
25.6
Strings and Lists
• split() breaks a string into parts and produces a list of
strings. We think of these as words.
• We can access a particular word or loop through all the
words

>>> abc = 'With three words'


>>> stuff = abc.split() >>> print(stuff)
>>> print(stuff) ['With', 'three', 'words']
['With', 'three', 'words'] >>> for w in stuff:
>>> print(len(stuff)) print(w)
3 With
>>> print(stuff[0]) Three
With Words
>>>
Strings and Lists
• By default, the delimiter is >>> line = 'A lot of spaces'
>>> etc = line.split()
spaces >>> print(etc)
['A', 'lot', 'of', 'spaces']
• When you do not specify a >>>
delimiter, multiple spaces >>> line = 'first;second;third'
>>> thing = line.split()
are treated like one >>> print(thing)
delimiter ['first;second;third']
>>> print(len(thing))
• You can specify what 1
>>> thing = line.split(';')
delimiter character to use >>> print(thing)
in the splitting ['first', 'second', 'third']
>>> print(len(thing))
3
>>>
The Double Split Pattern
• Sometimes we split a line one way, and then grab one of the
pieces of the line and split that piece again

line = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'

words = line.split() stephen.marquard@uct.ac.za


email = words[1]
print(email)

pieces = email.split('@') uct.ac.za


print(pieces[1])
Summary
• Lists and definite loops
• Indexing and lookup
• List mutability
• Functions: len, min, max, sum
• Slicing lists
• List methods: append, remove
• Sorting lists
• Splitting strings into lists of words
• Using split to parse strings
Exercise 1
• Build a function to find the list of words that are longer
than n from a given string. For example, in a string
“Hope to make some good friends in the MSBA
program”, find the words that are longer than 4
characters.
Dictionaries
Dictionaries
• A dictionary is like a list, but more general. In a list, the index
positions have to be integers; in a dictionary, the indices can
be (almost) any type.
• You can think of a dictionary as a mapping between a set of
indices (which are called keys) and a set of values. Each key
maps to a value.
• The association of a key and a value is called a key-value pair
or sometimes an item.
• As an example, we’ll build a dictionary that maps from
English to Spanish words, so the keys and the values are all
strings.
Dictionaries
• Dictionary use curly braces and have a list of key: value pairs
• You can make an empty dictionary using empty curly braces

>>> eng2sp = dict()


>>> print(eng2sp)
{}

>>> jjj = {'chuck' : 1 , 'fred' : 42, 'jan': 100}


>>> print(jjj)
{'jan': 100, 'chuck': 1, 'fred': 42}
>>> ooo = { }
>>> print(ooo)
{}
Dictionaries
• Lists index their entries based on the position in the list
• Dictionaries have no order
• So we index the “value” we put in the dictionary with a
“key”

>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'}


eng2sp['one']

'uno'

• The order of the key-value pairs may not be the same

>>> print(eng2sp)
{'one': 'uno', 'three': 'tres', 'two': 'dos'}
Dictionaries
• Dictionaries are like lists except that they use keys instead of
index numbers to look up or add key-value pairs

>>> lst = list() >>> ddd = dict()


>>> lst.append(21) >>> ddd['age'] = 21
>>> lst.append(183) >>> ddd['course'] = 182
>>> print(lst) >>> print(ddd)
[21, 183] {'course': 182, 'age': 21}
>>> lst[0] = 23 >>> ddd['age'] = 23
>>> print(lst) >>> print(ddd)
[23, 183] {'course': 182, 'age': 23}

We can change
the value of a key
Dictionary Tracebacks
• It is an error to reference a key which is not in the dictionary
>>> ccc = dict()
>>> print(ccc['csev'])
Traceback (most recent call last):
File "<stdin>", line 1, in
<module>
KeyError: 'csev'

• We can use the in operator to see if a key is in the


dictionary

>>> 'csev' in ccc


False
Dictionary as a set of counters
• If we encounter a new name, we need to add a new entry in
the dictionary.
• If it’s not a new name, we simply add one to the count in
the dictionary under that name
word = 'brontosaurus'
counts = dict()
for c in word:
if c not in counts:
counts[c] = 1
else:
counts[c] = counts[c] + 1
print(counts)

{'a': 1, 'b': 1, 'o': 2, 'n': 1, 's': 2, 'r': 2, 'u': 2, 't': 1}


Dictionaries Methods get()
• Dictionaries have a method called get() that takes a key
and a default value. If the key appears in the dictionary, get
returns the corresponding value; otherwise it returns the
default value.
• The following is equivalent.

if name in counts:
x = counts[name]
x = counts.get(name, 0)
else:
x = 0

Default value if key does not exist


Dictionaries Methods get()
• We can use get() to write our letter counter more
concisely.
• Because the get() method automatically handles the case
where a key is not in a dictionary, we can reduce four lines
down to one and eliminate the if statement.

word = 'brontosaurus'
counts = dict()
for c in word:
counts[c] = counts.get(c,0) + 1
print(counts)
Counting Words in Text
• The general pattern to count the words in a line of text is to
split the line into words, then loop through the words and
use a dictionary to track the count of each word
independently.

line = input('')
line = line.lower()
words = line.split()

We will learn how to read from files soon.


Counting Words in Text

text:
the clown ran after the car and the car ran
into the tent and the tent fell down on the
clown and the car

counts = dict()
for word in text:
counts[word] = counts.get(word,0) + 1
print('Counts', counts)

Counts {'and': 3, 'on': 1, 'ran': 2, 'car': 3, 'into': 1, 'after':


1, 'clown': 2, 'down': 1, 'fell': 1, 'the': 7, 'tent': 2}
Definite Loops and Dictionaries
• Even though dictionaries are not stored in order, we can
write a for loop that goes through all the entries in a
dictionary - actually it goes through all of the keys in the
dictionary and looks up the values

>>> counts = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}


>>> for key in counts:
print(key, counts[key])

jan 100
chuck 1
fred 42
Looping through Keys in Order
• Recall: items (key-value pairs) in a dictionary are un-
ordered, i.e., you never get the items from a dictionary in
any predictable order. However, this is not a problem:

favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python'}

for name in sorted(favorite_languages.keys()):


print(name.upper() + ', thank you.')
Retrieving Lists of Keys and Values
• You can get a sequence of keys, values, or items (both) from
a dictionary by calling methods keys(), values(), and
items()
>>> jjj = {'chuck' : 1 , 'fred' : 42, 'jan': 100}
>>> print(jjj.keys())
dict_keys(['chuck', 'fred', 'jan'])
>>> print(jjj.values())
dict_values([1, 42, 100])
>>> print(jjj.items())
dict_items([('chuck', 1), ('fred', 42), ('jan',
100)])>>>

• Using list() to >>> print(list(jjj))


get a list of keys ['jan', 'chuck', 'fred']
Two Iteration Variables
• We loop through the key-value pairs in a dictionary using
*two* iteration variables
• Each iteration, the first variable is the key and the second
variable is the corresponding value for the key

jjj = {'chuck' : 1 , 'fred' : 42, 'jan': 100}


for aaa, bbb in jjj.items() :
print(aaa, bbb)

jan 100
chuck 1
fred 42
Removing Key-Value Pairs
• When we no longer need a piece of information that’s
stored in a dictionary, we can use the del statement
completely remove a key-value pair.

alien = {'color': 'green', 'points': 5}


print(alien)
del alien['points']
print(alien)

{‘color’: ‘green’, ‘points’: 5}


{‘color’: ‘green’}
Lists in a dictionary
• The values of a dictionary can also be lists.
departments = {
'business': ['accounting','finance','economics'],
'linguistics': ['chinese','english','japanese']
}
departments['linguistics'][2]
'japanese'

for college, majors in departments.items(): business


print(college) accounting
for i in majors: finance
print(' ',i) economics
linguistics
chinese
english
japanese
Dictionaries in a dictionary
• The values of a dictionary can also be dictionaries.

people = {
1: {'Name': 'John', 'Age': '27', 'Sex': 'Male'},
2: {'Name': 'Marie', 'Age': '22', 'Sex': 'Female'}
}

• To loop through the value of the inner dictionary


for i in people.values():
print(i['Name'])

John
Marie
Summary
• Dictionary as a set of counters
• Dictionaries Methods get
• Counting Words in Text
• Looping through Keys in Order
• Removing Key-Value Pairs
• Lists in a dictionary
• Dictionaries in a dictionary
Exercise 2
• Write a program to print a dictionary where the keys are
numbers between 1 and 10 (both included) and the values
are square of keys.
Exercise 3
• Write a program to extract and list all the mayors in the
following dictionary.
fav_city= {
'New York': {'Country': 'USA', 'Population': 8.5, 'Mayor': 'de Blasio'},
'Paris': {'Country': 'France', 'Population': 2.2, 'Mayor': 'Hidalgo'},
'Tokyo': {'Country': 'Japan', 'Population': 9.3, 'Mayor': 'Koike'}
}

• So result should be

the mayor of New York is de Blasio


the mayor of Paris is Hidalgo
the mayor of Tokyo is Koike
Tuples
Tuples Are Like Lists
• Tuples are another kind of sequence that functions much
like a list - indexed starting at 0
• Creating a tuple is simple: we just need to put different
comma-separated values. Or, put them in parentheses
>>> tup1 = ('english', 'french', 'spanish', 1997, 2000)
>>> tup2 = (1,2,3,4,5)
>>> tup3 = 'a', 'b', 'c', 'd'
>>> tup4 = (); # an empty tuple
>>> tup5 = tuple(); # another empty tuple

• To create a tuple containing a single element, you have to


include a comma
>>> tup5=(50, )
but... Tuples are “immutable”
• Unlike a list, once you create a tuple, you cannot alter its
contents - similar to a string

>>> x = [9, 8, 7] >>> y = 'ABC' >>> z = (5, 4, 3)


>>> x[2] = 6 >>> y[2] = 'D' >>> z[2] = 0
>>> print(x) Traceback:'str' Traceback:'tuple'
>>>[9, 8, 6] object does object does
>>> not support item not support item
Assignment Assignment
>>> >>>
Things not to do With Tuples

>>> x = (3, 2, 1)
>>> x.sort()
Traceback:
AttributeError: 'tuple' object has no attribute 'sort'
>>> x.append(5)
Traceback:
AttributeError: 'tuple' object has no attribute 'append'
>>> x.reverse()
Traceback:
AttributeError: 'tuple' object has no attribute 'reverse'
>>>
A Tale of Two Sequences
• Although list and tuple look alike, they are very different.

>>> l = list()
>>> dir(l)
['append', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort']

>>> t = tuple()
>>> dir(t)
['count', 'index']
Tuples are More Efficient
• Since Python tuple structures are immutable, they are
simpler and more efficient in terms of memory use and
performance than lists
• So in our program when we are making “temporary
variables” we prefer tuples over lists
Tuples and Assignment
• We can also put a tuple on the left-hand side of an
assignment statement
• We can even omit the parentheses

>>> (x, y) = (4, 'fred')


>>> print(y)
fred
>>> a, b = 99, 98
>>> print(a)
99
Tuples and Dictionaries
• The items() method in dictionaries returns a sequence of
(key, value) tuples

>>> d = {'a':10, 'b':1, 'c':22}


>>> t = list(d.items())
>>> print(t)
[('b', 1), ('a', 10), ('c', 22)]
Tuples are Comparable
• The comparison operators work with tuples and other
sequences. If the first item is equal, Python goes on to the
next element, and so on, until it finds elements that differ.

>>> (0, 1, 2) < (5, 1, 2)


True
>>> (0, 1, 2000000) < (0, 3, 4)
True
>>> ('Jones', 'Sally') < ('Jones', 'Sam')
True
>>> ('Jones', 'Sally') > ('Adams', 'Sam')
True
Sorting Lists of Tuples
• We can take advantage of the ability to sort a list of tuples
to get a sorted version of a dictionary
• First we sort the dictionary by the key using the items()
method and sorted() function

>>> d = {'a':10, 'b':1, 'c':22}


>>> d.items()
dict_items([('a', 10), ('c', 22), ('b', 1)])
>>> sorted(d.items())
[('a', 10), ('b', 1), ('c', 22)]
Sort by Values Instead of Key
• If we could construct a list of tuples of the form (value, key)
we could sort by value
• We do this with a for loop that creates a list of tuples

>>> c = {'a':10, 'b':1, 'c':22}


>>> tmp = list()
>>> for k, v in c.items():
tmp.append((v, k))

>>> print(tmp)
[(10, 'a'), (22, 'c'), (1, 'b')]
>>> tmp = sorted(tmp, reverse=True)
>>> print(tmp)
[(22, 'c'), (10, 'a'), (1, 'b')]
Even Shorter Version

>>> c = {'a':10, 'b':1, 'c':22}


>>> print(sorted([(v,k) for k,v in c.items()]))
[(1, 'b'), (10, 'a'), (22, 'c')]

[(1, 'b'), (10, 'a'), (22, 'c')]


Conversions: Lists and Tuples
• We can convert a list to a tuple using the built-in tuple()
function.
>>> mylist =[4,5,6]
>>> tuple(mylist)
(4,5,6)

• Similarly, we can convert a tuple into a list using the built-in


list() function.
>>> mytuple=(4,5,6)
>>> list(mytuple)
[4,5,6]
Summary
• Tuple syntax
• Immutability
• Comparability
• Sorting
• Tuples in assignment statements
• Sorting dictionaries by either key or value
Exercise 4
• Sort the following text by the length of each word in
descending order.
txt = 'Writing programs is a very creative and rewarding
activity'

You might also like