>> y = [int(i=='c' or i=='g') for i in x] >>> y [0, 1, 0, 1, 0, 1, 0, 1, 1, 0]"> >> y = [int(i=='c' or i=='g') for i in x] >>> y [0, 1, 0, 1, 0, 1, 0, 1, 1, 0]">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Python – Part III Dr. Nancy Warter-Perez.

Similar presentations


Presentation on theme: "An Introduction to Python – Part III Dr. Nancy Warter-Perez."— Presentation transcript:

1 An Introduction to Python – Part III Dr. Nancy Warter-Perez

2 Introduction to Python – Part III2 Overview 2-D Lists List comprehensions Zip File I/O Split Functions Programming Workshop #3

3 Introduction to Python – Part III3 Python List Comprehensions 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]

4 Introduction to Python – Part III4 Creating 2-D Lists 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 2 nd row and 3 rd column of L L[2][3] = 5

5 Introduction to Python – Part III5 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

6 Introduction to Python – Part III6 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

7 Introduction to Python – Part III7 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}

8 Introduction to Python – Part III8 File I/O To open a file myfile = open('pathname', ) modes: 'r' = read 'w' = write Ex: infile = open("D:\\Docs\\test.txt", 'r') Ex: outfile = open("out.txt", 'w') – in same directory

9 Introduction to Python – Part III9 Common input file operations OperationInterpretation input = open ('file', 'r')open input file S = input.read()read entire file into string S S = input.read(N)Read N bytes (N>= 1) S = input.readline()Read next line L = input.readlines()Read entire file into list of line strings

10 Introduction to Python – Part III10 Common output file operations OperationInterpretation output = open('file', 'w')create output file output.write(S)Write string S into file output.writelines(L)Write all line strings in list L into file output.close()Manual close (good habit)

11 Introduction to Python – Part III11 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).

12 Introduction to Python – Part III12 Split Ex: >>> x = "a,b,c,d" >>> x.split(',')['a', 'b', 'c', 'd'] >>> x.split(',',2)['a', 'b', 'c,d'] Ex: >>> y = "533a4" >>> y.split()['5', '33', 'a', '4']

13 Introduction to Python – Part III13 Functions Function definition def adder(a, b, c): return a+b+c Function calls adder(1, 2, 3) -> 6

14 Introduction to Python – Part III14 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

15 Introduction to Python – Part III15 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

16 Introduction to Python – Part III16 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.

17 Introduction to Python – Part III17 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/ ftp://ftp.ncbi.nih.gov/blast/matrices/ Due Date: Thursday, November 8 th (must submit to get an extension) One Week Automatic Extension: Thursday, November 15 th

18 Introduction to Python – Part III18 Example Scoring Matrix File

19 Introduction to Python – Part III19 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.


Download ppt "An Introduction to Python – Part III Dr. Nancy Warter-Perez."

Similar presentations


Ads by Google