Lists 2 Day /19/14 LING 3820 & 6820 Natural Language Processing

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists Introduction to Computing Science and Programming I.
COMPUTATION WITH STRINGS 4 DAY 5 - 9/05/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
UNICODE DAY /22/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Structured programming 3 Day 33 LING Computational Linguistics Harry Howard Tulane University.
COMPUTATION WITH STRINGS 2 DAY 2 - 8/29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 9: Tuples and lists.
REGULAR EXPRESSIONS 4 DAY 9 - 9/15/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
COMPUTATION WITH STRINGS 1 DAY 2 - 8/27/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
REGULAR EXPRESSIONS 2 DAY 7 - 9/10/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
REGULAR EXPRESSIONS 1 DAY 6 - 9/08/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
CONTROL 2 DAY /26/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
COMPUTATION WITH STRINGS 3 DAY 4 - 9/03/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
CONTROL 3 DAY /29/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
String and Lists Dr. José M. Reyes Álamo.
Chapter 1 Logic and Proof.
Lists 1 Day /17/14 LING 3820 & 6820 Natural Language Processing
Tuples and Lists.
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Containers and Lists CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Computation with strings 2 Day 3 - 9/02/16
Flat text 2 Day 7 - 9/14/16 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
Computation with strings 3 Day 4 - 9/07/16
Computation with strings 1 Day 2 - 8/31/16
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Learning to Program in Python
Regular expressions 2 Day /23/16
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
control 4 Day /01/14 LING 3820 & 6820 Natural Language Processing
Bryan Burlingame 17 October 2018
CS190/295 Programming in Python for Life Sciences: Lecture 6
Data types Numeric types Sequence types float int bool list str
8 – Lists and tuples John R. Woodward.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Data Structures – 1D Lists
Control 3 Day /05/16 LING 3820 & 6820 Natural Language Processing
NLP 2 Day /07/16 LING 3820 & 6820 Natural Language Processing
String and Lists Dr. José M. Reyes Álamo.
Python Data Structures: Lists
Regular expressions 3 Day /26/16
Lists Part 1 Taken from notes by Dr. Neil Moore
Reference semantics, variables and names
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Bryan Burlingame 13 March 2019
Python Data Structures: Lists
Python Data Structures: Lists
15-110: Principles of Computing
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Computation with strings 4 Day 5 - 9/09/16
Python Review
CSE 231 Lab 6.
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Control 1 Day /30/16 LING 3820 & 6820 Natural Language Processing
Introduction to Computer Science
Presentation transcript:

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