Function for list

Download Report

Transcript Function for list

3rd day
Functions for string, list and dictionary
File input & output

.split()
◦ Divide string by delimiter
◦ Usage)

.strip()
◦
◦
◦
◦
Remove both side gap of string
.rstrip() – right strip
.lstrip() – left strip
Usage)

.replace(‘a’,’b’)
◦ Replace ‘a’ to ‘b’
◦ Usage)

.zfill()
◦ Filling 0
 Ex) 001, 0035
◦ Usage)

.append()
◦ Add elements to end of list
◦ Usage)

.insert()
◦ Add elements to specific location
◦ Usage)

.remove()
◦ Remove element
◦ Usage)

.pop()
◦ Retrieve and remove element
◦ Usage)

.join()
◦ Join elements of list as string
◦ Usage)

set() & list()
◦ Remove redundancy
◦ Usage)

.keys() & .values()
◦ Retrieve keys and values of dictionary as lists
◦ Usage)

open()
◦ Usage)
 open(file_path, ‘w’) for writing
 open(file_path, ‘r’) for reading
◦ Reading file
 .read()
 .readline()
 .readlines()

.read() – read entire file

.readline() – read one line for each command

.readlines() – read all line as list

Use .readlines() with for loop

File write
1.
2.
3.
test file로 "This is a sequence file"이라는 내용을 담은 file을 "title.txt" 이름으로 작성한다.
"title.txt"를 open하여 그 내용 중 첫번째 행을 읽어 들여 내용을 화면에 출력한다. 출력내용 : "The first line is :
"
임의의 단백질 FASTA file(/data2/python_study/ap2.fa)을 open하여 전체 내용을 읽어 들여서 화면에 출력
( while사용 )
4.
문제 16번에서 화면출력대신 "seq2.txt"에 출력한다.
5.
문제 17번에서 2번째 단어를 출력한다. 출력내용 : "The second word is : "
6.
7.
8.
문제 16번에서 첫 번째 라인은 'title'변수, 두 번째 라인부터는 "seq"변수에 저장하여 개행 문자 빼고 화면에 출력
한다
.
문제 19번에서 읽어 들인 seq에서 loop로 지정된 위치 다음에 오는 3개의 아미노산 출력한다. 계속 반복하여 물
어본다 (종료:XXX ). (seq의 첫 번째 자리부터 번역) Q&A) Enter the position : 15 The next 3 amino acids are :
KRN
문제 19번에서 특정 아미노산이 발견되는 위치를 화면상에 출력한다. 계속 반복하여 물어본다 ( 종료:XXX ). (seq
의 첫 번째 자리부터 번역) Q&A) Enter amino acid code : M The amino acid is found at : 19, 22