Hossain Shahriar [email protected]. Announcement and reminder! Tentative date for final exam...
-
Upload
helen-hudson -
Category
Documents
-
view
212 -
download
0
Transcript of Hossain Shahriar [email protected]. Announcement and reminder! Tentative date for final exam...
CISC 101: Fall 2011
Hossain [email protected]
Announcement and reminder!Tentative date for final exam need to be fixed!
Topics to be covered in this lecture(s)ListFileRegular expression
ListA flexible data structure that can store one or
more itemsL = [1, 2, 3]
Some useful methods of listlist.append(x): Add an item to the end of the listlist.insert(i, x): Insert an item at a given position list.remove(x): Remove the first item whose value
is x. It is an error if there is no such item (use with exception)
List (cont.)list.pop([i]): Remove the item at the given position in the
list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.
list.index(x): Return the index in the list of the first item whose value is x.
list.count(x): Return the number of times x appears in the list.
list.sort(): Sort the items of the list, in place.
list.reverse(): Reverse the elements of a list.
List -operators (cont.)Slice operator allows to choose a subset from a listL = [1, 2, 3, 4, 5, 6]
Slice [ :3] = [4, 5, 6] #select elements from 0 to (3-1) Slice [3 :3] = [ ] #select elements from 3 to (3-1) Slice [2 : ] = [3, 4, 5, 6] #select elements from 2 to (6-1)
Concatenating two lists (+) L1= [1, 2, 3], L2= [4, 5, 6] L = L1+ L2 = [1, 2, 3, 4, 5, 6]
Multiplication (*)L1= [1, 2, 3]L2 = L1*2 = [1, 2, 3, 1, 2, 3]
List -operators (cont.)del operator deletes an element
L = [1, 3, 5, 7] del L[1] = [1, 5, 7]
in tests whether an element is in a list or not Returns true or false
L = [1, 3, 5, 7] 3 in L = true 3 not in L = false # not operator
File Stores data permanently How to read a file?
Open a file Read data by each line Close the file Do these steps in try-exception blocks
def fileread(name): try: f = open(name, "r") lines = f.readlines() f.close() for line in lines: print line except IOError: raise ValueError("Filename does not exist, or cannot be read.")
fileread("C:/Users/shahriar/Desktop/CISC101/test.txt")
FileHow to write a file?
Open a fileWrite dataClose the fileDo the steps in try-exception blocks
def filewrite(name): try: f = open(name, “w") # this will erase any previous content f.write(‘hello world') f.close() except IOError: raise ValueError("Filen cannot be open for writing")
fileread("C:/Users/shahriar/Desktop/CISC101/test.txt")
FileMore one opening mode
f = open(name, “w") 'r‘: file will be opened for read only 'w‘: file will be opened for writing only and it will erase
all the contents if a file already exists 'a‘: fill will be opened for appending; any data written to
the file is automatically added to the end
Regular expressionVery useful for string data processing
TokenizationFirst, we import regular expression library re
We need to know how to write regular expressionMatch any sequence of alpha numeric characters– ‘\
w+’str = “I know that you do not know”Mylist = re.findall (‘\w+’, str) ['I', 'know', 'that', 'you', 'do', 'not', 'know']We have just performed string tokenization!!Each element of the list is a token that we obtained from
strWe could have obtained Mylist using str.split() too!
Regular expression\d – identifies all numbers in a string and return in
a list
How can we recognize numbers in a string? -- \d+numlist = re.findall ('\d+', "i am 19 years old ")print numlist>>> [19]
Take home assignmentCan you find two more examples of meta
characters and show examples how they provide outputs?
Regular expression\d: Matches any decimal digit; [0-9].\D: Matches any non-digit character; [^0-9].\s: Matches any whitespace character; [ \t\n\r\f\v].\S: Matches any non-whitespace character; [^ \t\
n\r\f\v].\w: Matches any alphanumeric character; [a-zA-
Z0-9_].\W: Matches any non-alphanumeric character;
[^a-zA-Z0-9_].
+ : One or more character*: zero or more character
Regular expressionmatch(): Determine if the RE matches at the beginning of
the string.search(): Scan through a string, looking for any location
where this RE matches.findall(): Find all substrings where the RE matches, and
returns them as a list.