Transcript list

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
1
Creating Sequences
• Creations
– Strings
• Use quotes:
s = ""
– Lists
• Use brackets
• list1 = []
• list2 = [ 1, 7, 66 ]
– Tuples
•
•
•
•
Use parentheses
tuple1 = ()
Tuple2 = 5,
tuple3 = ( 19, 27 )
2
for item in .. ?
Two ways of accessing all elements in a sequence
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
Adding items to a list
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)
• 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 )
Using Lists and Tuples
• Only difference between lists and tuples is:
– Lists are mutable, tuples are immutable
>>>
>>>
>>>
[3,
aList = [3, 6, 9]
aList[2] = 141
aList
6, 141]
>>> aTuple = (3, 6, 9)
>>> aTuple[2] = 141
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
7
passing_lists_to_functions.py
Passing a list as an argument to a function
•
sum and max functions
8
passing_lists_to_functions.py
Output
Top 3 of the grades [10, 5, 11, 5, 13, 5] are:
[13, 11, 10]
Average grade: 5
9
passing_lists_to_functions.py
Explanation
[ , , , , , ]
grades
10
5
11
5
13
5
10
passing_lists_to_functions.py
Explanation
a
[ , , , , , ]
grades
10
5
11
5
13
5
11
Better way of finding topN from a list of numbers
Algorithm:
Don’t find the max N times: Inefficient!
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
12
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
topN.py
top 11 10 5 4
n
5
i
13
topN.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
14
Functions working on lists
•
•
•
•
map
reduce
filter
zip
15
longest_string_in_list.py
Unelegant way of finding length of longest string
Don’t call len
twice!
16
longest_string_in_list2.py
Elegant way using map
•
map: applies given function (first argument) to all elements in
given sequence (second argument), returns list with results
17
Sequence unpacking – testing a function
gcd_function_test.py
•
for/else construct
19
gcd_function2_test.py
New implementation of function, same test
20
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
21
gcd_function_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
listIterate
and all
valuess,0 update dictionary value
through
for each dinucleotide key
Dictionary method iteritems:
Iterate through (a sorted copy of) all of d’s
Add report string to
key/value pairs.
list,
The keys are tuples, the values histogram
are integers.
convert to string
and return
22
gcd_function_test.py
Output
Input DNA string: atcagcgatattcgatcagctag
AG :
3
AT :
4
CA :
2
CG :
2
CT :
1
GA :
2
GC :
2
TA :
2
TC :
3
TT :
1
23
On to the exercises..
25