LING 681 Intro to Comp Ling
Download
Report
Transcript LING 681 Intro to Comp Ling
Q9
Structured programming
Day 31
LING 681.02
Computational Linguistics
Harry Howard
Tulane University
Course organization
http://www.tulane.edu/~ling/NLP/
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
3
Structured programming
NLPP §4
Assignment 1
>>> foo = 'Monty'
>>> bar = foo
>>> foo = 'Python'
>>> bar
'Monty'
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
5
Assignment 2
>>> foo = ['Monty', 'Python']
>>> bar = foo
>>> foo[1] = 'Bodkin'
>>> bar
['Monty', 'Bodkin']
Why?
The second line copies a reference to the object 'bar',
not its content.
Use id() to find the numerical identifier of each
variable.
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
6
List assignment and
computer memory
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
7
Assignment 3
>>> empty = []
>>> nested = [empty, empty, empty]
>>> nested
[[], [], []]
>>> nested[1].append('Python')
>>> nested
[['Python'], ['Python'], ['Python']]
The third line creates a nested list,
in which each element is a reference to the same list.
use id() to find the identifier of each one.
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
8
Assignment 4
>>> nested = [[]] * 3
>>> nested[1].append('Python')
>>> nested[1] = ['Monty']
>>> nested
[['Python'], ['Monty'], ['Python']]
Shows the difference between modifying an via an object
reference and overwriting an object reference.
Use id() to find the identifier of each one.
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
9
Equality
>>> size = 5
>>> python = ['Python']
>>> snake_nest = [python] * size
>>> snake_nest[0] == snake_nest[1] ==
snake_nest[2] == snake_nest[3] == snake_nest[4]
True
>>> snake_nest[0] is snake_nest[1] is
snake_nest[2] is snake_nest[3] is snake_nest[4]
True
is tests for object identity.
Check by:
[id(snake) for snake in snake_nest]
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
10
Equality 2
>>> import random
>>> position = random.choice(range(size))
>>> snake_nest[position] = ['Python']
>>> snake_nest
[['Python'], ['Python'], ['Python'], ['Python'], ['Python']]
>>> snake_nest[0] == snake_nest[1] == snake_nest[2] ==
snake_nest[3] == snake_nest[4]
True
>>> snake_nest[0] is snake_nest[1] is snake_nest[2] is
snake_nest[3] is snake_nest[4]
False
Find the interloper:
[id(snake) for snake in snake_nest]
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
11
Conditionals
>>> mixed = ['cat', '', ['dog'], []]
>>> for element in mixed:
...
if element:
...
print element
...
cat
['dog']
In the condition part of an if statement, a
nonempty string or list is evaluated as true, while
an empty string or list evaluates as false.
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
12
Conditionals 2
What's the difference between using if...elif as opposed to using a couple of if statements
in a row?
>>> animals = ['cat', 'dog']
>>> if 'cat' in animals:
...
print 1
... elif 'dog' in animals:
...
print 2
...
1
Since the if clause of the statement is satisfied, Python never tries to evaluate the elif
clause, so we never get to print out 2.
By contrast, if we replaced the elif by an if, then we would print out both 1 and 2.
So an elif clause potentially gives us more information than a bare if clause; when it
evaluates to true, it tells us not only that the condition is satisfied, but also that the
condition of the main if clause was not satisfied.
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
13
Universal quantification
>>> sent = ['No', 'good', 'fish', 'goes',
'anywhere', 'without', 'a', 'porpoise',
'.']
>>> all(len(w) > 4 for w in sent)
False
>>> any(len(w) > 4 for w in sent)
True
The functions all() and any() can be applied to a
list (or other sequence) to check whether all or any
items meet some condition.
09-Nov-2009
LING 681.02, Prof. Howard, Tulane University
14
Next time
Continue §4