week 3: lists, input and output files, working on sequences/strings
week 3: lists, input and output files, working on sequences/strings CSE 6613
Popular in Bio-computing
verified elite notetaker
Popular in Buttler Hall
This 5 page Class Notes was uploaded by Marina Notetaker on Wednesday August 31, 2016. The Class Notes belongs to CSE 6613 at Mississippi State University taught by Andy Perkins in Fall 2016. Since its upload, it has received 8 views. For similar materials see Bio-computing in Buttler Hall at Mississippi State University.
Reviews for week 3: lists, input and output files, working on sequences/strings
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 08/31/16
August 29, 2016 1) Working on lists: Scales are single values, while lists are multiple values List is a list of things and it can include numbers and/or letters (strings). List is given by characters separated by a coma inside [ ]. Make a list: list = [ x,z,y] Lists must have [ ] on either side a. Location of a character on a list: list [location] Note: in this list, number 1 is in the position 0. If you ask for a character in a position that does not exist on the list, it will give an error. b. How many specific character you have repeating on a list: list.count(character) c. To add a character on your list: list.append(character). The new character will always be included at the end of your list. d. To remove a character from your list: list.remove(character). It will always remove the first specific character from your list. e. Give the characters located at specific point: list [start: end]. Don’t forget that the end point is excluded from the list. In this case, on my list a have only once the character 1 and twice the 52, for example. Hello is a string and was inserted in the list by the end of the list. The first 52 on the list was removed, keeping the last one. Just like string operations, you can get the location or find the location from a particular character. So you can get a slice from a list: List [ :x] = gives the list elements from the beginning to x. List [x: ]= giver the list from x to the end of the list. f. Addition between lists, also called concatenate: list2= a + [characters you want to include]. Remember it will include in your first list (a), at the end of that. g. List = 2*other list, means that you do two copies of a list. h. Length of a list: len(list) i. Finding location of strings (index), you do: list.index(‘string name’). So index tell you that your string ‘hello’ is located at 4 place on the list. You can also try to find a string. If the string is not on the list it will tell you, for example ‘hi’. j. Removing the last character from a list: list.pop( ). Or removing a character from a particular location list.pop(location). So, it’s different from list.remove(character), because if you have more than one of this character, you can’t tell which one will be removed. Before removing a character from a list, you can save this character. So for that: save=list.pop ( ) So save will be the name of the last character of the list. You can add again this character by list.append(save). k. Organizing the list in numerical or alphabetical order: list.sort( ). If you have strings with numbers, all elements become alphabetical (includingthe numbers) and in this case you would be a different order. Ex: 1, 17, 5, hello, hi. In this case 17 is read like 1.7. If you have 2 lists, one rely on another because they are supposed to have same characters, so list ‘a = b’. If you do sort in one list, automatically the other one will have same fade. To save the original order from a list: Beforesorting,writethatlist=list1.copy(),then youcansortthelist1andtheoriginal list will keep the same. So it this example, ‘a’ was sorted, while ‘b’ kept the same original order by using b=a.copy( ). l. Deleting characters from specific location: del list [star: end]. Other ways include: list.remove( ) or list.pop( ). 2) Getting the identifier from a list id(list) = gives you the ID from your list. If you have two lists that as equal ‘a=b’, both lists (‘a’ and ‘b’) will have the same ID. List= [ ] , means that you just opened a list that is empty. Anytime you change your list, it will have a new ID. To clean a list, you can also use list=[ ]. 3) How to work and open files on your python Save a file in .txt extension. For example, 5 DNA sequences. a. Open the file: f=open (‘name of file.extension’, ‘r’). Every time you open a file, don’t forget to close it! b. Close a file: f.close( ) c. Print every line from a file use: “for line in f:”, “print (line)”. The problem with this command is that it will print on the keyboard by jumping one line at each written line. d. Taking out the spaces between lines in the keyboard: You have two options! Line = line.strip( ) Print (line, end= ‘ ‘) DON’T USE BOTH TOGETHER! If you use both together it will put together strings from different lines. August 31, 2016 1) Working on a file Another example of file, that should be saved as “.txt” extension: a. Open a file: f = open (‘file name’, ‘r’), ‘r’ means that you can only read the file, not change it. f= open (‘file name’, ‘w’), ‘w’ means you can write the file and change it. f= open (‘file name’, ‘a’), ‘a’ means b. Asking to enter the file’s name on the keyboard: f= input (‘file name to read:’) c. Calculating mean (average), standard deviation and total length for each sequence. Standard deviation is the squared root of variance (not written in there!). Must be deviation = stdr(variance). d. Turning your sequences into a list. Each line becomes a sequence Lines = [line.strip() for line in open (‘file name’)] = you turn each line into a strip, so then they will be a list inside a list. And you can print each line separately and analyze each one separately. e. Outputting files As you can see, this codes include: Opening a file, called inf, just for read (r) a. Give a name to your output. Outfile = ‘name.txt’ b. Opening an output file that will have the name ‘output.txt’, in which you will write (w). Ouf = open (‘file name’, ‘w’) c. To write your output file with each character in a different line you add ‘\n’. d. To insert any information in your output file you must write file name.write( whatever you want) You can insert as many as output files as you want. This example has 3. Always: - Name your output file: outfile = ‘name.txt’ - Open your file: ouf=open (outfile, ‘w’) - Print whatever you want in the file: ouf.write(…) This oufwrite commands to write the specific line/ string/ list, : and the length of the line. ‘\n’ means that if more than one line/ string will be written, it you go to a second line in the output.
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'