1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable...
-
date post
19-Dec-2015 -
Category
Documents
-
view
233 -
download
0
Transcript of 1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable...
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
2
Creating Sequences
• Creations– Strings
• Use quotes: s = ""
– Lists• Use brackets• list1 = []• list2 = [ 1, 7, 66 ]
– Tuples• Use parentheses• tuple1 = ()• Tuple2 = 5,• tuple3 = ( 19, 27 )
startlist = [‘John’,‘George’, …,‘Wayne’] # long list
for i, name in enumerate( startlist ): # here we get both the subscript and the item: print “%s has index %d” %( name, i )
for name in startlist: # here we get the item directly, but not the subscript print name
for item in .. ?
Two ways of accessing all elements in a sequence
aList = [] # create empty list
# add values to list
for number in range( 1, 11 ):
aList += [ 1.0/number ]
Adds the values to the list (by adding a new list with one element to the existing list in each round)
Adding items to a list
• Using the + operator, you need to add a list to a list:
aList += [ number ]
• Using the append method, you add an element to the list:
aList.append( number )
7
Using Lists and Tuples
• Only difference between lists and tuples is:– Lists are mutable, tuples are immutable
>>> aList = [3, 6, 9]>>> aList[2] = 141>>> aList[3, 6, 141]
>>> 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
23gcd_
func
tion_
test
.py
Output
Input DNA string: atcagcgatattcgatcagctagAG : 3AT : 4CA : 2CG : 2CT : 1GA : 2GC : 2TA : 2TC : 3TT : 1
25
On to the exercises..