1 Sequences •A sequence is a list of elements • Lists and tuples – Lists mutable – Tuples immutable • Sequence elements can be indexed with subscripts – First index is 0 – Access element with sequenceName[ subscript ] – Subscripts may be negative – Slicing works same way as with strings
21
Embed
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Sequences
• A sequence is a list of elements
• Lists and tuples– Lists mutable
– Tuples immutable
• Sequence elements can be indexed with subscripts– First index is 0
– Access element with sequenceName[ subscript ]– Subscripts may be negative
>>> aTuple = (3, 6, 9)>>> aTuple[2] = 141Traceback (most recent call last): File "<stdin>", line 1, in ?TypeError: object doesn't support item assignment
8pass
ing_
lists
_to_
func
tions
.py
Passing a list as an argument to a function
• sum and max functions
9pass
ing_
lists
_to_
func
tions
.py
Output
Top 3 of the grades [10, 5, 11, 5, 13, 5] are:[13, 11, 10]Average grade: 5
10pass
ing_
lists
_to_
func
tions
.py
Explanation
[ , , , , , ]grades
10 5 11 5 135
11pass
ing_
lists
_to_
func
tions
.py
Explanation
[ , , , , , ]grades
10 5 11 5 135
a
12
Better way of finding topN from a list of numbers
1. Keep a sorted list of current top-N
2. Go through given list one number at a time
3. If number is greater than current N’th greatest,
insert in correct position
Algorithm:
Don’t find the max N times: Inefficient!
13topN
.py
Better way of finding topN from a list of numbers
Want to compare to top‘s N’th element, so top must be initialized with N values
Make sure any value can enter top
top 11 10 5 4
5n i
14topN
.py
Output
- current top4: [10, 4, 4, 4] - current top4: [10, 5, 4, 4] - current top4: [11, 10, 5, 4] - current top4: [11, 10, 5, 5] - current top4: [13, 11, 10, 5]Top 4 of the grades [10, 5, 11, 5, 13, 5] are:[13, 11, 10, 5]Average grade: 8.17
15
Functions working on lists
• map
• reduce
• filter
• zip
16long
est_
strin
g_in
_lis
t.py
Unelegant way of finding length of longest string
Don’t call len twice!
17long
est_
strin
g_in
_lis
t2.p
yElegant way using map
• map: applies given function (first argument) to all elements in given sequence (second argument), returns list with results
19gcd_
func
tion_
test
.py
Sequence unpacking – testing a function
• for/else construct
20gcd_
func
tion2
_tes
t.py
New implementation of function, same test
21
Dictionaries
• Dictionaries– Mapping that consists of unordered key-value pairs– Each value is referenced though its key – Curly braces {} are used to create a dictionary– Creating a dictionary: { key1:value1, … }
or: dict( [ (key1, value1), …] )– Keys must be immutable values such as strings,
numbers and tuples– Values can be anything– Add item to dictionary d with d[key] = value
22gcd_
func
tion_
test
.py
Dictionary example: DNA dinucleotides
List comprehension:Create list of all tuples a, b where a and b are from nuc
Dictionary method fromkeys:Create dictionary with keys from the given list and all values 0Iterate through s, update dictionary
value for each dinucleotide key
Dictionary method iteritems:Iterate through (a sorted copy of) all of d’s key/value pairs. The keys are tuples, the values are integers.
Add report string to histogram list, convert to string and return