Upload
octavia-lucas
View
226
Download
3
Embed Size (px)
Citation preview
Computation with strings 4Day 5 - 9/05/14LING 3820 & 6820
Natural Language Processing
Harry Howard
Tulane University
Course organization
05-Sept-2014NLP, Prof. Howard, Tulane University
2
http://www.tulane.edu/~howard/LING3820/
The syllabus is under construction.
http://www.tulane.edu/~howard/CompCultEN/
Is there anyone here that wasn't here on Wednesday?
3.5. Practice, cont
4. What is the longest sequence of operators that you can make?
5. Show which of len(), sort(), and set() takes the most time to process.
6. Line 2 from the previous subsection has the expression L[2:6].capitalize().upper(). Show whether it is faster for Python to process this as it is, or broken into its parts as in:>>> A = L[2:6]>>> B = A.capitalize()>>> C = B.upper()
7. Recall the discussion of the default precedence of * and + in §3.1.1. Perhaps * comes first because it is quicker to process than +. Test this hypothesis by writing a function for either combination of * and + and time them to see which runs faster.
05-Sept-2014NLP, Prof. Howard, Tulane University
6
A string is a sequence of characters delimited between single or double quotes.
§3. Computation with strings
05-Sept-2014
7
NLP, Prof. Howard, Tulane University
3.6. Dealing with strings that are longer than a line>>> longslash = 'A wonderful bird is the pelican.'\
... 'His bill can hold more than his belican.'
>>> print longslash
'A wonderful bird is the pelican.His bill can hold more than his belican.'
>>> longparen = ('A wonderful bird is the pelican.'
... 'His bill can hold more than his belican.')
>>> print longparen
'A wonderful bird is the pelican.His bill can hold more than his belican.'
05-Sept-2014NLP, Prof. Howard, Tulane University
8
Strings longer than a line, cont.>>> longsingle = '''A wonderful bird is the pelican.... His bill can hold more than his belican.'''>>> print longsingleA wonderful bird is the pelican.His bill can hold more than his belican.>>> longdouble = """A wonderful bird is the pelican.... His bill can hold more than his belican.""">>> print longdoubleA wonderful bird is the pelican.His bill can hold more than his belican.>>> longdouble'A wonderful bird is the pelican.\nHis bill can hold more than his belican.'
05-Sept-2014NLP, Prof. Howard, Tulane University
9
3.7.1. Assignment of variable names You have by now performed an assignment of a variable to an expression several times and hopefully have internalized something like:
variable = expression This can be read as “variable is assigned to expression”.
For convenience, we often refer to the variable as the name for the expression, though the Python documentation actually prefers identifier to name.
05-Sept-2014NLP, Prof. Howard, Tulane University
10
What's in a name
There are several limitations on what can be a name. The main one is that it cannot be one of the words that Python reserves for its own uses, such as:>>> print = 'print'
A trickier case is that of the methods built into Python, such as the string methods reviewed in this chapter:>>> len = 'len'
Using one of them as a name is not prohibited, but is considered bad form, since it could be confused with the method len() and lead to misunderstanding. Your really do not want to create the possibility of expressions like len(len) in your code.
05-Sept-2014NLP, Prof. Howard, Tulane University
11
What's in a name, cont. There are also limits on what characters can be part of a
name. A name must start with a letter or underscore, but not a digit:>>> name = 'zzz'>>> _name = 'zzz'>>> 0name = 'zzz'
Once a name has been started correctly, it can contain any combination of letters, digits, or underscores, but nothing else:>>> my_name = 'zzz'>>> my_name1 = 'zzz'>>> my name = 'zzz'>>> my-name = 'zzz'
Upper and lower case are different:>>> name = 'zzz'>>> Name = 'zzz2'>>> name == Name
05-Sept-2014NLP, Prof. Howard, Tulane University
12
3.7.2. Mutability
Try these:>>> name[0] = 'b'>>> del name[0]
Strings are immutable, which is to say, once a string has been created, it cannot be changed by adding or deleting items from it.
The only way that it can be changed is by means of the methods that were reviewed above.
05-Sept-2014NLP, Prof. Howard, Tulane University
13