Lists 2 Day 11 - 9/19/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University
Course organization http://www.tulane.edu/~howard/LING3820/ The syllabus is under construction. http://www.tulane.edu/~howard/CompCultEN/ NLP, Prof. Howard, Tulane University 19-Sept-2014
Review of Lists A list in Python is a sequence of objects delimited by square brackets, []. NLP, Prof. Howard, Tulane University 19-Sept-2014
Summary of string & list ops operation string S list L number of elements len(S) len(L) alphabetical order sorted(S) sorted(L) set formation set(S) set(L) concatenation S1+S2 L1+L2 duplication S*2 L*2 count duplicates S.count() L.count() find position S.index() L.index() slice S[] L[] conversion S.split() ‘’.join(L) NLP, Prof. Howard, Tulane University 19-Sept-2014
The inverse relation between splitting and joining NLP, Prof. Howard, Tulane University 19-Sept-2014
§5. Lists NLP, Prof. Howard, Tulane University 19-Sept-2014
Open Spyder NLP, Prof. Howard, Tulane University 19-Sept-2014
5.2.2. Strings and lists share the sequence type Strings and lists share the count(), index() and [] operations because they are both members of Python’s sequence type In a nutshell, a sequence type has an index associated with every one of its elements. It is the indexation which endows the type with the ability to be counted, retrieved by position and sliced. NLP, Prof. Howard, Tulane University 19-Sept-2014
Sets are not sequences Sets are not indexed, so they do not permit counting (obviously, since all the duplicates are removed), retrieval by position or slicing. All of the following operations produce an error: # reassign L if it is not available: L = 'Love looks not with the eyes, but with the mind.'.split() >>> setL = set(L) >>> setL.count('the') >>> setL.index('with') >>> setL[0] Thus sets have almost the ‘opposite’ properties of lists, being unorderd and without duplicates. NLP, Prof. Howard, Tulane University 19-Sept-2014
5.3. How strings and lists differ # reassign L if it is not available: L = 'Love looks not with the eyes, but with the mind.'.split() >>> L.append('Awesome!') >>> L >>> L.extend("You tell'em, Will!".split()) >>> L.insert(5,'bloodshot') >>> L.remove('bloodshot') >>> L.pop(5) NLP, Prof. Howard, Tulane University 19-Sept-2014
None of these apply to strings >>> S = ' '.join(L) >>> S.append('Awesome!') >>> S.extend("You tell'em, Will!".split()) >>> S.insert(5,'bloodshot') >>> S.remove('not') >>> S.pop(5) NLP, Prof. Howard, Tulane University 19-Sept-2014
Lists are mutable Do you know why these methods fail on a string? Recall our discussion of mutability. We observed that, once a string is created, its constituency cannot be altered. In Pythonese, such an object is called immutable. What does the first block of code in this section say about the mutability of a list? Well, ``L`` is clearly mutable; every method above changes its constituents. So here we have the principle difference between the two types, strings are immutable while lists are not. Thus if you need a data type whose elements will wax and wane over the course of your program, a list is the way to go. NLP, Prof. Howard, Tulane University 19-Sept-2014
5.3.1. How to reverse and randomize the order of a list Given its mutability, not only can elements be added or subtracted from a list, but they also should be able to be moved around. Try this: >>> L2 = "If you can't be with the one you love, love the one you're with.".split() >>> L2.reverse() >>> L2 >>> L2[::-1] reverse(), it comes as no surprise, reverses the order of elements in a list. What is a surprise, though, is that it does so “in place”. That is, it reverses the list within the method, rather than sending the reversal as output to a new list. Thus the original order is destroyed. It is easy enough to recover, by reversing the reversal, or using the slice trick that you are reminded of in line 4. NLP, Prof. Howard, Tulane University 19-Sept-2014
The original order But what if you wanted to use the original order at the same time? It seems logical to just assign the original list to a new list and reverse it, so that you wind up with two lists, one in the original order and one reversed. Don’t just sit there, try it out: # recover the original order by reversing what you just did >>> L2.reverse() >>> L3 = L2 >>> L3.reverse() >>> L3 >>> L2 NLP, Prof. Howard, Tulane University 19-Sept-2014
The official solution # If necessary, recover the original order by reversing what you just did >>> L2.reverse() >>> from copy import copy >>> L4 = copy(L2) >>> L4.reverse() >>> L4 >>> L2 NLP, Prof. Howard, Tulane University 19-Sept-2014
The sneaky solution >>> L5 = L2[:] >>> L5.reverse() NLP, Prof. Howard, Tulane University 19-Sept-2014
Shuffling, again >>> L6 = copy(L2) >>> L6 >>> from random import shuffle >>> shuffle(L6) >>> L2 NLP, Prof. Howard, Tulane University 19-Sept-2014
Next time Q3 Unicode NLP, Prof. Howard, Tulane University 19-Sept-2014