200 likes | 352 Views
An Introduction to Python – Part III. Dr. Nancy Warter-Perez. Overview. 2-D Lists List comprehensions Zip File I/O Split Functions Programming Workshop #3. Precise way to create a list Consists of an expression followed by a for clause, then zero or more for or if clauses Ex:
E N D
An Introduction to Python – Part III Dr. Nancy Warter-Perez
Overview • 2-D Lists • List comprehensions • Zip • File I/O • Split • Functions • Programming Workshop #3 Introduction to Python – Part III
Precise way to create a list Consists of an expression followed by a for clause, then zero or more for or if clauses Ex: >>> [str(round(355/113.0, i)) for i in range(1,6)] ['3.1', '3.14', '3.142', '3.1416', '3.14159'] Ex: replace all occurrences of G or C in a string of amino acids with a 1 and A and T with a 0 >>> x = "acactgacct" >>> y = [int(i=='c' or i=='g') for i in x] >>> y [0, 1, 0, 1, 0, 1, 0, 1, 1, 0] Python List Comprehensions Introduction to Python – Part III
To create a 2-D list L, with C columns and R rows initialized to 0: L = [[]] #empty 2-Dlist L = [[0 for col in range(C)] for row in range(R)] To assign the value 5 to the element at the 2nd row and 3rd column of L L[2][3] = 5 Creating 2-D Lists Introduction to Python – Part III
Zip – for parallel traversals • Visit multiple sequences in parallel • Ex: >>> L1 = [1,2,3] >>> L2 = [5,6,7] >>> zip(L1, L2) [(1,5), (2,6), (3,7)] • Ex: >>> for(x,y) in zip(L1, L2): … print x, y, '--', x+y 1 5 -- 6 2 6 -- 8 3 7 -- 10 Introduction to Python – Part III
More on Zip • Zip more than two arguments and any type of sequence • Ex: >>> T1, T2, T3 = (1,2,3),(4,5,6),(7,8) >>> T3 (7,8) >>> zip(T1, T2, T3) [(1,4,7),(2,5,8)] -- truncates to shortest sequence Introduction to Python – Part III
Dictionary Construction with zip • Ex: >>> keys = ['a', 'b', 'd'] >>> vals = [1.8, 2.5, -3.5] >>> hydro = dict(zip(keys,vals)) >>> hydro {'a': 1.8, 'b': 2.5, 'd': -3.5} Introduction to Python – Part III
To open a file myfile = open('pathname', <mode>) modes: 'r' = read 'w' = write Ex: infile = open("D:\\Docs\\test.txt", 'r') Ex: outfile = open("out.txt", 'w') – in same directory File I/O Introduction to Python – Part III
Common input file operations Introduction to Python – Part III
Common output file operations Introduction to Python – Part III
Extracting data from string – split • String.split([sep, [maxsplit]]) - Return a list of the words of the string s. • If the optional argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). • If the argument sep is present and not None, it specifies a string to be used as the word separator. • The optional argument maxsplit defaults to 0. If it is nonzero, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements). Introduction to Python – Part III
Split • Ex: >>> x = "a,b,c,d" >>> x.split(',')['a', 'b', 'c', 'd'] >>> x.split(',',2)['a', 'b', 'c,d'] • Ex: >>> y = "5 33 a 4" >>> y.split()['5', '33', 'a', '4'] Introduction to Python – Part III
Functions • Function definition • def adder(a, b, c): return a+b+c • Function calls • adder(1, 2, 3) -> 6 Introduction to Python – Part III
Functions – Polymorphism >>>def fn2(c): … a = c * 3 … return a >>> print fn2(5) 15 >>> print fn2(1.5) 4.5 >>> print fn2([1,2,3]) [1,2,3,1,2,3,1,2,3] >>> print fn2("Hi") HiHiHi Introduction to Python – Part III
Functions - Recursion def fn_Rec(x): if x == []: return fn_Rec(x[1:]) print x[0], y = [1,2,3,4] fn_Rec(y) >>> 4 3 2 1 Introduction to Python – Part III
Programming Workshop #3 • Create a text file called "test1.txt" with the following data: # Sample data 1 2 3 4 5 # more data 6 7 8 9 10 • Create another text filed called "test2.txt" with the following data: # More test data # With more header info A B C D E F G • Write a script to do the following • 1. Prompt the user for a filename • 2. Open the file • 3. Read the file into a list of strings. • 4. If the line does not begin with a '#' print the line to the screen. • Test your script on test1.txt and test2.txt. Introduction to Python – Part III
Programming Homework #2P • Write a program to prompt the user for a scoring matrix file name and read the data into a dictionary • Download a representative set of PAM and Blossum Scoring Matrix Files • Scoring matrices should be downloaded from ftp://ftp.ncbi.nih.gov/blast/matrices/ • Due Date: Thursday, November 8th (must submit to get an extension) • One Week Automatic Extension: Thursday, November 15th Introduction to Python – Part III
Example Scoring Matrix File Introduction to Python – Part III
Algorithm for Homework 2P • Step 1: Create an empty list (of dictionaries) • Step 2: Prompt the user for the scoring matrix file name • Step 3: Open the file and read the contents as a list of strings. Ignore the comment lines • Step 4: When you reach a line that doesn’t start with '#' read in the amino acid symbols and split them into your keys for your dictionary • Step 5: Read in the rest of the lines one at a time. For each line: • Step 5a. Slice off the first character (amino acid). • Step 5b. For the rest of the string split into individual numbers and convert to a list of integers (use a list comprehension). This is your data for your dictionary. • Step 5c. Zip the keys and data together and convert into a dictionary. • Step 5d. Add the dictionary to the list of dictionaries • Step 6: After you’ve read all lines, create the dictionary of dictionaries by zipping the keys and the list of dictionaries and convert into a dictionary. Introduction to Python – Part III