Sequence Types - cool.ntu.edu.tw
Transcript of Sequence Types - cool.ntu.edu.tw
Sequence Types
Computer Programming (EE3031), Fall 2021
Jiun-Lang Huang, GIEE, NTU
EE3031, F21 Jiun-Lang Huang, NTUEE
Sequence Types
• In Python, sequence is the generic term for an ordered collection of values.
• Basic sequence types: list, tuple, and range.
• Text sequence type: str
• Binary sequence types: bytes, bytearray, memoryview
2
List
EE3031, F21 Jiun-Lang Huang, NTUEE
Creating a List
• A list is a sequence of arbitrary values.
• sequence ordered
• Ways to create lists.
• Enclosing comma-separated items with square brackets.
• Use the list() function.
• ...
⇒
4
>>> members = ['John','Mary','Tom'] >>> record = [['Bond','James'],26,'EE'] >>> empty_list = [] >>> >>> empty_list = list() >>> empty_list [] >>> list_from_range = list(range(3)) >>> list_from_range [0, 1, 2] >>> list_from_string = list('hello') >>> list_from_string ['h', 'e', 'l', 'l', 'o'] >>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Basic List Operations
• Use + to concatenate lists.
• Creating a new list without modifying the original ones.
• Use * to repeat lists: k * a_list or a_list * k.
5
>>> boys = ['John','Tom','Peter'] >>> girls = ['Helen','Mary'] >>> all = boys + girls >>> all ['John', 'Tom', 'Peter', 'Helen', 'Mary'] >>>
>>> one = [1] >>> some_ones = one * 3 >>> some_ones [1, 1, 1] >>> many_ones = 3 * some_ones >>> many_ones [1, 1, 1, 1, 1, 1, 1, 1, 1] >>>
Jiun-Lang Huang, NTUEEEE3031, F21
• Use the len() function to get the number of items in a list.
6
>>> boys = ['John','Tom','Peter'] >>> girls = ['Helen','Mary'] >>> len(boys) 3 >>> len(girls) 2 >>> print('There are %d boys and %d girls in the class' ... % (len(boys),len(girls))) There are 3 boys and 2 girls in the class >>> len(list()) 0 >>>
Jiun-Lang Huang, NTUEEEE3031, F21
• Membership test:
• True if an item of a_list is equal to x, else False.
7
x in a_list
>>> members = ['John','Mary','Tom','Helen']
>>> visitor = 'Tom'
>>> if visitor in members:
... print('welcome!')
...
welcome!
>>> visitor = 'Joe'
>>> if visitor not in members:
... print('please register first.')
...
please register first.
Jiun-Lang Huang, NTUEEEE3031, F21
• min(s) / max(s)
• Return the smallest / biggest value in s.
• s.index(x[,i[,j]])
• Return the index of the first occurrence of x in s (at or after index i and before index j).
8
>>> scores = [70, 43, 50, 90, 65, 89]
>>> max(scores)
90
>>> min(scores)
43
>>>
>>> numbers = [20,30,14,20,19,20]
>>> numbers.index(20)
0
>>> numbers.index(20,4)
5
>>> numbers.index(20,2,5)
3
>>> numbers.index(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 0 is not in list
>>>
Jiun-Lang Huang, NTUEEEE3031, F21
• s.count(x)
• Return the total number of occurrences of x in s.
9
>>> names = ['Joe','John','Mary','Joe','Tom']
>>> names.count('Joe')
2
>>> names.count('Mary')
1
>>> names.count('Peter')
0
>>>
EE3031, F21 Jiun-Lang Huang, NTUEE
List Indexing
• Each item in a list can be accessed by its index with the bracket operator.
• Indices are integers, from 0 to len()-1.
• An out-of-range index causes IndexError.
10
>>> work_days = ['Mon','Tue','Wed','Thu','Fri'] >>> work_days[0] 'Mon' >>> work_days[3] 'Thu' >>> work_days[5] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>>
Jiun-Lang Huang, NTUEEEE3031, F21
• Negative indices
• -1 yields the last item, -len() the first item.
• -0 is regarded as 0.
11
>>> digits = [1,2,3,4,5]
>>> digits[-1]
5
>>> digits[-5]
1
>>> digits[-6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>>
* In fact, Python does the conversion for you — the positive index is obtained by adding len() to a negative index.* Examples:
-1 + 5 4-5 + 5 0
→→
EE3031, F21 Jiun-Lang Huang, NTUEE
List Slicing
• A pair of indices, separated by a colon, identifies a contiguous section of a list, i.e., a sublist.
• left_ind:
• The lower bound, inclusive.
• Default to 0 if omitted.
• right_ind:
• The upper bound, non-inclusive.
• Default to len() if omitted.
12
>>> odds = [1,3,5,7,9]
>>> odds[0:3]
[1, 3, 5]
>>> odds[2:5]
[5, 7, 9]
>>> odds[:3]
[1, 3, 5]
>>> odds[3:]
[7, 9]
>>>
a_list[left_ind,right_ind]
Jiun-Lang Huang, NTUEEEE3031, F21
• Out of range slice indices are handled gracefully.
• If out of range, replaced by either 0 or len().
13
>>> odds = [1,3,5,7,9]
>>> odds[1:100]
[3, 5, 7, 9]
>>> odds[-100:3]
[1, 3, 5]
>>> odds[-100:100]
[1, 3, 5, 7, 9]
>>> odds[300:400]
[]
>>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Updating List Element(s)
• s[i] = x
• Replace item i of s by x.
•s[i:j] = t
• Replace slice of s from i to j by contents of the iterable t.
14
>>> record = ['math',90,'physics',93,'chemistry',85]
>>> record[1] = 91
>>> record
['math', 91, 'physics', 93, 'chemistry', 85]
>>> record[-2] = 'calculus'
>>> record
['math', 91, 'physics', 93, 'calculus', 85]
>>> record[2:4] = ['english',70]
>>> record
['math', 91, 'english', 70, 'calculus', 85]
>>>
* len(t) can be different from j-i.
EE3031, F21 Jiun-Lang Huang, NTUEE
Adding Elements to List
• s.append(x)
• Append x to the end of s.
• s.extends(t) or s += t
• Extend s with the contents of iterable t.
•s.insert(i,x)
• Insert x into s at the index given by i.
15
>>> odds = [1]
>>> odds.append(3)
>>> odds
[1, 3]
>>> odds.extend([7,9])
>>> odds
[1, 3, 7, 9]
>>> odds.insert(2,5)
>>> odds
[1, 3, 5, 7, 9]
>>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Removing List Elements
•del s[i:j]
• Remove elements of s[i:j] from s.
•s.remove(x)
• Remove the first x from s.
•s.pop([i])
• Return and remove the item at i.
• i defaults to -1.
•s.clear()
• Remove all items from s.
16
>>> odds = [1,3,5,7,9,11,13]
>>> del odds[0]
>>> odds
[3, 5, 7, 9, 11, 13]
>>> del odds[1:3]
>>> odds
[3, 9, 11, 13]
>>> odds.remove(9)
>>> odds
[3, 11, 13]
>>> odd = odds.pop()
>>> odd
13
>>> odds
[3, 11]
>>> odds.clear()
>>> odds
[]
>>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Modifying List Order
•s.sort(reverse=False)
• Sort s in place in ascending order.
• Set reverse to True for descending order.
•s.reverse()
• Reverse the items of s in place.
•sorted(s,reverse=False)
• Return a new sorted list from the items in s.
17
>>> odds = [3,7,5,1,9]
>>> odds.reverse()
>>> odds
[9, 1, 5, 7, 3]
>>> odds.sort(reverse=True)
>>> odds
[9, 7, 5, 3, 1]
>>> odds.insert(1,11)
>>> odds
[9, 11, 7, 5, 3, 1]
>>> new_odds = sorted(odds)
>>> odds
[9, 11, 7, 5, 3, 1]
>>> new_odds
[1, 3, 5, 7, 9, 11]
>>>
Tuple
EE3031, F21 Jiun-Lang Huang, NTUEE
What is a tuple?
• Roughly like a list, except that it is immutable.
• For the purpose of representing a fixed collection of values.
• Ways to construct tuples:
• A pair of parentheses denotes an empty tuple: ().
• Using a trailing comma for a singleton tuple: a, or (a,).
• Separating items with comma(s): a, b, c or (a, b, c).
• Use the tuple() function: tuple() or tuple(iterable).
19
Jiun-Lang Huang, NTUEEEE3031, F21 20
>>> t = ()
>>> type(t)
<class 'tuple'>
>>> t
()
>>> colors = 'red','green','blue'
>>> colors
('red', 'green', 'blue')
>>> letters = tuple('abcde')
>>> letters
('a', 'b', 'c', 'd', 'e')
>>> days = tuple(['M','T','W','T','F'])
>>> days
('M', 'T', 'W', 'T', 'F')
>>> series = tuple(range(1,5,2))
>>> series
(1, 3)
>>>
Jiun-Lang Huang, NTUEEEE3031, F21 21
>>> (0,1,2) + (4,5)
(0, 1, 2, 4, 5)
>>> digits = (0,1,2,3,4,5,6,7,8,9)
>>> min(digits)
0
>>> max(digits)
9
>>> 10 in digits
False
>>> len(digits)
10
>>> digits[3]
3
>>> digits[2:5]
(2, 3, 4)
>>> digits.index(3)
3
>>> digits.count(0)
1
>>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Why Tuples?
• Their immutability is the main point.
• Benefits
• Tuples are faster than lists.
• Protect data against accidental changes, e.g., days of a week.
• Tuples can be used as keys in dictionaries, while lists cannot.
22
EE3031, F21 Jiun-Lang Huang, NTUEE
Tuple Assignment, Packing, and Unpacking
• In Python, you can assign multiple values with one single assignment statement.
• A very convenient way to swap values.
• In packing, we put values into a new tuple, while in unpacking, we extract those into variables.
23
>>> records = 'John','farmer',25
>>> name,occupation,age = records
>>> print(name,occupation,age)
John farmer 25
>>>
>>> a,b = 10,20
>>> print(a,b)
10 20
>>> a,b = b,a
>>> print(a,b)
20 10
>>>
String
EE3031, F21 Jiun-Lang Huang, NTUEE
Python String Literals
• String is a sequence of characters.
• Single quotes and double quotes strings.
• Use double quotes if the string contains the single quote character, and vice versa.
25
>>> brand = 'Ford' >>> model = "Focus" >>> print(brand,model) Ford Focus
>>> string_with_quote = "It's not true." >>> string_with_quote = 'Python has a "print" function.'
EE3031, F21 Jiun-Lang Huang, NTUEE
Escape Sequences
• Backslashes are used to introduce special character codings, known as escape sequences.
• Some commonly used ones:
26
escape sequence meaning
\\ backslash
\' single quote
\" double quote
\n newline
\t horizontal tab
Jiun-Lang Huang, NTUEEEE3031, F21
• Examples
27
>>> print('\\\\\\') \\\ >>> print('It\'s true.') It's true. >>> print("The \"print\" function.") The "print" function. >>> print('Hi, there!\nWhere are you going today?') Hi, there! Where are you going today? >>> print('Monday\t20\nTuesday\t27') Monday 20 Tuesday 27 >>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Triple Quoted String Literal
• For convenient coding of multiline text data, e.g., docstring.
• Syntax
• Begins with three quotes (of either the single or double variety),
• followed by any number of lines of text, and
• closed with the same triple-quote sequence that opened it.
28
>>> msg = """line1 ... line2 ... line3""" >>> msg 'line1\nline2\nline3' >>> print(msg) line1 line2 line3 >>>
EE3031, F21 Jiun-Lang Huang, NTUEE
The str() Function
• Return a string version of the given object.
29
>>> str(1) '1' >>> str(12.345) '12.345' >>> str(True) 'True' >>> str(range(2)) 'range(0, 2)' >>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Strings Are Immutable!
• You cannot change an existing string.
• You can only create a new string to replace the old one (by assignment).
30
>>> name = 'cindy'
>>> name[1] = 'a' # try to change name to 'candy'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item
assignment
>>>
>>> name = 'ca' + name[2:]
>>> name
'candy'
>>>
EE3031, F21 Jiun-Lang Huang, NTUEE
General String Operations
• Support most list operations.
31
>>> 'Hello,' + 'Peter!' 'Hello,Peter!' >>> '*' * 8 '********' >>> 5 * '=' '=====' >>>
>>> msg = 'Hello' >>> min(msg) 'H' >>> max(msg) 'o' >>> 'lo' in msg True >>> msg.index('l') 2 >>> msg.count('l') 2 >>> msg[0] 'H' >>> msg[2:] 'llo' >>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Some String Methods
• s.join(iterable)
• Return a string which is the concatenation of the strings in iterable, separated by s.
• s.lower() / s.upper()
• Return a copy of s with all the cased characters converted to lowercase / uppercase.
• s.replace(old,new[,count])
• Return a copy of s with all occurrences of substring old replaced by new.
• If count is given, only the first count occurrences are replaced.
32
Check Python standard library for more.
>>> members = ['John','Tom','Joe'] >>> ', '.join(members) 'John, Tom, Joe' >>> 'YES'.lower() 'yes' >>> 'banana'.replace('na','ba') 'bababa' >>>
Jiun-Lang Huang, NTUEEEE3031, F21
• s.split()
• Return a list of the words in the string using white spaces as the delimiter string.
• Runs of consecutive whitespace are regarded as a single separator.
• The result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
• One can specify the separator by setting the sep parameter; however, the splitting algorithm will be different.
33
>>> scores = ' 12 8 9 \t 3 \n 10' >>> scores.split() ['12', '8', '9', '3', '10'] >>>
Jiun-Lang Huang, NTUEEEE3031, F21
• s.strip()
• Return a copy of the string with the leading and trailing white space removed.
• Use s.lstrip() or s.rstrip() to remove only the leading or trailing whitespace characters.
• Can also specify the characters to strip.
34
>>> ' this is a big apple.\t '.strip() 'this is a big apple.' >>> ' this is a big apple.\t '.rstrip() ' this is a big apple.' >>> ' this is a big apple.\t '.lstrip() 'this is a big apple.\t ' >>>
>>> 'mississippi'.strip('mip') 'ssiss' >>> 'mississippi'.rstrip('mip') 'mississ' >>> 'mississippi'.lstrip('mip') 'ssissippi' >>>
Traversing a Sequence
EE3031, F21 Jiun-Lang Huang, NTUEE
Traversing with for Statement
• The items in seq is assigned to var one by one.
36
for var in seq: for_blk
>>> msg = 'Good morning!' >>> o_count = 0 >>> for c in msg: ... if c == 'o': ... o_count += 1 ... >>> print(o_count) 3 >>>
>>> msg = input('enter some numbers: ') enter some numbers: 3 10 90 20 78 >>> tokens = msg.split() >>> tokens ['3', '10', '90', '20', '78'] >>> total = 0 >>> for token in tokens: ... total += int(token) ... >>> print('average = %.2f' % (total/len(tokens))) average = 40.20 >>>
EE3031, F21 Jiun-Lang Huang, NTUEE
Traversing with Indices
• Sometimes we need to know the item index while traversing a sequence.
• Use range() to produce the indices.
37
>>> winners = ['John','Mary','Peter','Joe'] >>> for index in range(len(winners)): ... print('%d: %s' % (index+1,winners[index])) ... 1: John 2: Mary 3: Peter 4: Joe >>>
Jiun-Lang Huang, NTUEEEE3031, F21
• Use the enumerate() function to obtain indices and values at the same time.
38
>>> names = ['John','Mary','Tom','Helen'] >>> for index,name in enumerate(names): ... print('rank %d: %s' % (index+1,name)) ... rank 1: John rank 2: Mary rank 3: Tom rank 4: Helen >>>