1 / 44

LING 408/508: Computational Techniques for Linguists

LING 408/508: Computational Techniques for Linguists. Lecture 4 8/27/2012. Outline. List creation and indexing List operators Built-in functions applied to lists List methods References and mutability Short assignment #3. Python datatypes. Primitive data types

nimrod
Download Presentation

LING 408/508: Computational Techniques for Linguists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LING 408/508: Computational Techniques for Linguists Lecture 4 8/27/2012

  2. Outline • List creation and indexing • List operators • Built-in functions applied to lists • List methods • References and mutability • Short assignment #3

  3. Python datatypes • Primitive data types • Integers, floating-point, Booleans, None • Atomic objects, cannot be decomposed into smaller pieces • Compound data types • Lists, sets, strings, dictionaries • Made up of smaller components

  4. Creating lists a = [1, 2, 3] # list of integers b = [True, False] # list of booleans c = ['a', 'b', 'cde'] # list of strings d = [7, 'cat', False] # can mix types e = [] # empty list f = [[1,2,3], [4,5,6]] # list of 2 lists

  5. list constructor (built-in function) >>> L = list() # same as L = [] >>> L [] >>> L = list([1,2,3]) >>> L [1,2,3] >>> L = list((1,2,3)) # pass in a tuple >>> L [1,2,3] • (When we get to object-oriented programming, you’ll learn what constructors are)

  6. Positions in a list • Positive indexing • Negative indexing 1 2 3 4 0 'a' 'b' 'c' 'd' 'e' -4 -3 -2 -1 -5

  7. Positive indexing >>> L = ['a', 'b', 'c', 'd', 'e'] >>> L[0] 'a' >>> L[1] 'b' >>> L[2] 'c' >>> L[3] 'd' >>> L[4] 'e' 1 2 3 4 0 'a' 'b' 'c' 'd' 'e'

  8. Negative indexing 1 2 3 4 0 >>> L[-1] 'e' >>> L[-2] 'd' >>> L[-3] 'c' >>> len(L) # built-in function length 5 >>> L[len(L)-1] # access last element 'e' 'a' 'b' 'c' 'd' 'e' -4 -3 -2 -1 -5

  9. Indexing recursively structured lists >>> [[1,2,3], [4,5,6]][1] [4,5,6] >>> [[1,2,3], [4,5,6]][1][0] 4 >>> [[1,2,3], [4,5,6]][-1][-3] 4 >>> [[[1]]][0] [[]] >>> [[[1]]][0][0] [1] >>> [[[1]]][0][0][0] 1

  10. Create new lists through slices • Doesn’t modify original list • Syntax: mylist[(start_idx):(stop_idx)(:step_size)] • start_idx • Begin accessing at this index (inclusive) • Default value: 0 • stop_idx • Stop accessing at this index (exclusive) • Default value: len(list) • step_size (optional): • Number of items to step through • Default: 1

  11. Create new lists through slices >>> L = ['a', 'b', 'c', 'd', 'e'] >>> L[:2] # up to but not including index 2 ['a', 'b'] >>> L[2:] # begin at index 2, through end ['c', 'd', 'e'] >>> L[2:5] ['c', 'd', 'e'] >>> L[2:4] ['c', 'd'] >>> L[:] # get a copy of entire list ['a', 'b', 'c', 'd', 'e'] 1 2 3 4 5 0 'a' 'b' 'c' 'd' 'e'

  12. Create new lists through slices >>> L[::2] # step size of 2 ['a', 'c', 'e'] >>> L[1::2] ['b', 'd'] >>> L[::1] # step size of 1: same list ['a', 'b', 'c', 'd', 'e'] >>> L[::-1] # step size of -1: reversed list ['e', 'd', 'c', 'b', 'a'] 1 2 3 4 0 'a' 'b' 'c' 'd' 'e'

  13. Negative step sizes >>> L = ['a', 'b', 'c', 'd', 'e'] >>> L[::-1] ['e', 'd', 'c', 'b', 'a'] >>> L[::-2] ['e', 'c', 'a'] >>> L[-1:-6:-1] ['e', 'd', 'c', 'b', 'a'] 1 2 3 4 0 'a' 'b' 'c' 'd' 'e' -6 -5 -4 -3 -2 -1

  14. Outline • List creation and indexing • List operators • Built-in functions applied to lists • List methods • References and mutability • Short assignment #3

  15. Assignment to item(s) of a list >>> L = [1,2,3] >>> L[0] = 6 >>> L [6, 2, 3] >>> L[:2] = [7,8] >>> L [7, 8, 3]

  16. Concatenation operators: +, += >>> L1 = [1,2,3] >>> L2 = [4,5] >>> L1 + L2 # create new list from L1 and L2 [1, 2, 3, 4, 5] >>> L1 # show that L1 wasn’t changed [1, 2, 3] >>> L1 = L1 + L2 >>> L1 [1, 2, 3, 4, 5] >>> L1 = [1, 2, 3] # reset L1 >>> L1 += L2 # L1 changes through assignment >>> L1 [1, 2, 3, 4, 5]

  17. List operator: *and operator overloading >>> [2,3] * 3 # repeat list 3 times [2, 3, 2, 3, 2, 3] • Operator overloading: an operator can be used on different data types, for different purposes >>> 3 + 5 # addition 8 >>> [1, 2] + [3] # concatenating lists [1, 2, 3] >>> 'U.S.' + 'A.' # string concatenation 'U.S.A.'

  18. List operators: in anddel >>> L = [1,2,3] >>> del L[1] # delete an item from list >>> L [1, 3] >>> 3 in L # test membership in list True >>> 7 in L False

  19. Outline • List creation and indexing • List operators • Built-in functions applied to lists • List methods • References and mutability • Short assignment #3

  20. Built-in functions: max, min, sum >>> L = [1,2,3,4,5,6] >>> max(L) 6 >>> min(L) 1 >>> sum(L) 19

  21. Built-in functions: len >>> L = [4,3,1,5,2] >>> len(L) 5 >>> len([]) # length of empty list 0

  22. Built-in functions: sorted >>> L = [4,3,1,5,2] >>> sorted(L) # returns a new list [1, 2, 3, 4, 5] >>> L # did not modify list [4, 3, 1, 5, 2] >>> L = sorted(L) # modify through assignment >>> L [1, 2, 3, 4, 5]

  23. rangefunction: get a sequence of integers • Syntax: range([start,] stop[, step]) • Returns an iterator; apply list function to get a list • Examples: >>> list(range(5)) # default start is 0 [0,1,2,3,4] >>> list(range(3,6)) [3,4,5] >>> list(range(3,8,2)) # positive step size [3,5,7] >>> list(range(9,0,-2)) # negative step size [9, 7, 5, 3, 1]

  24. Outline • List creation and indexing • List operators • Built-in functions applied to lists • List methods • References and mutability • Short assignment #3

  25. List methods • A method is a function called upon an object • Syntax, for lists: • L.methodname(parameters), where L is a list • Compare to with regular functions: functionname(parameters) • Some common list methods: • append, count, extend, index, insert, pop, remove, reverse, sort

  26. List methods: append, count, index >>> L = [1, 2, 2, 2] >>> L.append(9) # append a single element >>> L [1, 2, 2, 2, 9] >>> L.append([1,2,3]) # append a list >>> L [1, 2, 2, 2, 9, [1, 2, 3]] >>> L.count(2) # count occurrences of value 3 >>> L.index(9) # index of a value 4 >>> L.index(7) -1

  27. List methods: extend >>> L = [1, 2, 3] >>> L.extend([4, 5]) # extend with a list >>> L [1, 2, 3, 4, 5] >>> L += [4, 5] # extend is same as += >>> L [1, 2, 3, 4, 5, 4, 5]

  28. List methods: insert, pop, remove 0 1 2 3 4 >>> L = [1, 2, 3, 4, 5] >>> L.insert(7, 3) # insert item at an index [1, 2, 3, 7, 4, 5] >>> L.pop(0) # remove and return item at index 1 >>> L [2, 3, 7, 4, 5] >>> L.remove(7) # remove 1st occurrence of val >>> L [2, 3, 4, 5]

  29. List methods: reverse, sort >>> L = [3, 1, 5, 2, 4] >>> L.reverse() # reverse list, modify it >>> L [4, 2, 5, 1, 3] >>> L.sort() # sort the list, modify it >>> L [1, 2, 3, 4, 5] >>> L.sort(reverse=True) # sort in reverse >>> L # state value for param instead of use default [5, 4, 3, 2, 1]

  30. L.sort, sorted(L), and reversed(L) >>> L = [3,2,1] >>> sorted(L) # sorted function doesn’t change list [1, 2, 3] >>> L [3, 2, 1] >>> L2 = sorted([3,2,1]) # assign to new variable >>> L2 [1, 2, 3] >>> L.sort() # sort method changes the object >>> L [1, 2, 3] >>> list(reversed([1,2,3])) # return a reversed list [3, 2, 1]

  31. Watch out for None return value >>> L = [3, 1, 4, 2, 5] >>> L2 = L.sort() >>> print(L2) None • The sort method on a list object changes the list, but its return value is None

  32. Outline • List creation and indexing • List operators • Built-in functions applied to lists • List methods • References and mutability (adapts several slides by Guido van Rossum) • Short assignment #3

  33. Modification of variables:integers vs. lists x = 3 x = x + 1 y = [1,2,3] y.append(4) • Substantial differences in implementation!!!

  34. Variables are stored as references • What happens when we write x = 3 ? • Create an integer 3and store it in memory • Create a name x • xis associated with a reference to the memory location storing 3 • So, when we say that the value of xis 3, we mean that xnow refers to the integer 3 Type: Integer Data: 3 Name: x Ref: <address1> memory name list

  35. Modification of immutable types(int, string, float, bool, tuple) • x = x + 1 • The reference of name x is looked up. • The value at that reference is retrieved. • Calculate 3 + 1, producing 4, which is assigned to a new memory location with a new reference. • The name x is changed to point to this new reference. • The old data 3 is garbage collected if no name refers to it. • Immutable: change what a variable refers to, rather than change its value Type: Integer Data: 3 Name: x Ref: <address1> Type: Integer Data: 4

  36. Modification of mutable types(list, set, dictionary, classes) • y = [1,2,3] y.append(4) • The reference of name y is looked up. • Change the value at that memory location. • Mutables • Mutable: values are changed in place • We don’t copy them into a new memory address each time. • Why: mutables can take up a lot of memory. It would be inefficient to continually create new structures Type: List Data: Name: y Ref: <address1> 1 2 3 4

  37. Assignment for immutables vs. mutables >>> x = 3 >>> y = x # x and y refer to same data >>> x = 4 # x now refers to different data >>> y # y hasn’t changed 3 >>> x = [1,2,3] >>> y = x # x and y refer to same data >>> x[0] = 7 # modify data that x refers to >>> y # y is also changed [7, 2, 3]

  38. Assignment for immutables >>> x = 3 # create 3, name x refers to 3 >>> y = x # create name y, refers to a >>> x = 4 # create 4, ref of name x changes >>> y 3 Name: x Ref: <address1> Type: Integer Data: 3 Name: y Ref: <address2> Type: Integer Data: 4

  39. Assignment for mutables >>> x = [1,2,3] # create name x, refers to # values in memory >>> y = x # create name y, same ref. as x >>> x[0] = 7 # modify memory value >>> y [7, 2, 3] Name: x Ref: <address1> Type: List Data: 1 7 2 3 Name: y Ref: <address1>

  40. Copying a list • Assignment operator does not make a copy of an object • Common error: copying a reference rather than a list >>> x = [1,2,3] >>> y = x # wrong: y refers to same list as x >>> z = x[:] # right: make an entirely new list >>> z[0] = 5 >>> z [5,2,3] >>> x [1,2,3]

  41. In function calls, lists (and other mutables) are modified x = 7 def f(xx): xx = 3 print(x) # prints 7 f(x) print(x) # prints 7 L = [1,2,3] def g(LL): LL[0] = 7 print(L) # prints [1, 2, 3] g(L) print(L) # prints [7, 2, 3]

  42. Common programming error • Be careful not to modify mutables in functions, unless you really intend to • Reminder • Immutable types: int, string, float, bool, tuple • Mutable types: list, set, dictionary, classes

  43. Outline • List creation and indexing • List operators • Built-in functions applied to lists • List methods • References and mutability • Short assignment #3

  44. Due by beginning of class 8/29 • Let L = [1,2,3,4,5]. • For each of the following, give a one-line command involving multiple list operators, functions, and/or list methods such that L will have the given value. You may assume that L is re-set for each problem. More than one answer may be possible. [1,2,3,4,5,6] [0,1,2,3,4,5,6,7,8] [2,5] [1,2,3,2,3,4,3,4,5] [5,5,4,4,3,3,2,2,1,1] [1,2,3,[1,2,3,4,5],4,5] Note: [1, 2, 3, [...], 4, 5] is an incorrect result

More Related