Return to Video

Python for Informatics - Chapter 10 - Tuples

  • 0:00 - 0:05
    Hello and welcome to Chapter Ten of Python
    for Informatics, the chapter on tuples.
  • 0:05 - 0:10
    I'm Charles Severance. I'm your lecturer
    and I'm the author of the textbook.
  • 0:10 - 0:14
    As always, this material is copyright
    Creative Commons Attribution,
  • 0:14 - 0:18
    including the video lectures, the slides,
    and the book.
  • 0:20 - 0:24
    So tuples are the third kind of collection
    that we've talked about.
  • 0:24 - 0:26
    We've talked about lists and
  • 0:26 - 0:27
    we've talked about dictionaries.
  • 0:27 - 0:30
    And in the dictionary lecture, we kind
    of alluded to tuples.
  • 0:32 - 0:34
    We don't have to talk too much about
    tuples, it's really
  • 0:34 - 0:37
    shortening the lecture by telling you that
    they're a lot like lists.
  • 0:37 - 0:43
    They're a, non-change, they're a
    non-changeable list.
  • 0:45 - 0:49
    And, and the syntax of them is
  • 0:49 - 0:51
    pretty much the same as a list except
  • 0:51 - 0:56
    that we use parentheses instead of
    square brackets.
  • 0:56 - 0:58
    Okay? And so
  • 0:58 - 1:00
    like here is a three-tuple, a tuple
  • 1:00 - 1:03
    with three items in it, Glenn, Sally, and
    Joseph.
  • 1:03 - 1:06
    They are numbered zero, zero, one, and two.
  • 1:06 - 1:10
    So the second thing is one.
    So x sub two is indeed Joseph.
  • 1:12 - 1:17
    You know, we can pass them in as sequences
    to things like max or min or sum.
  • 1:19 - 1:22
    And so the maximum of 1, 9, 2 is 9.
  • 1:22 - 1:24
    And we can loop through them.
  • 1:24 - 1:26
    So here is y, it's a tuple.
  • 1:26 - 1:33
    It's 1, 9, 2. And iteration is going to
    through the three, three values, right?
  • 1:33 - 1:36
    And so it's going to print out 1, 9, 2,
    which runs the intended code once for
  • 1:36 - 1:39
    each of the values inside the tuple.
  • 1:39 - 1:41
    And so in this respect they're very much
    like lists.
  • 1:42 - 1:47
    But they're also different than lists in
    some real valuable ways.
  • 1:47 - 1:50
    Tuples are immutable, and so if you recall
  • 1:50 - 1:53
    when we talked about lists, we compared them to
    strings, because
  • 1:53 - 1:56
    both lists and strings are a sequence of
    elements where
  • 1:56 - 1:58
    the first one is zero, one, two, and
    et cetera.
  • 1:59 - 2:01
    But if we, if we look at a string for
  • 2:01 - 2:05
    example, and we have a three-character
    string A, B, C.
  • 2:05 - 2:09
    And we want to change the third character,
    y sub two, to D,
  • 2:09 - 2:12
    it complains and says, no, you can't do that.
  • 2:12 - 2:16
    But you can do it on a list, so if we have
    a list 9, 8, 7, and we
  • 2:16 - 2:18
    say x sub two is 6, which is the third
  • 2:18 - 2:22
    item, then the third item changes from
    7 to 6.
  • 2:22 - 2:23
    Okay?
  • 2:23 - 2:28
    So this is mutable.
    This is not mutable.
  • 2:31 - 2:36
    And tuples are also like not, are not
    mutable.
  • 2:36 - 2:42
    They're like strings, they're sort of like
    lists, in terms of what they can store.
  • 2:42 - 2:45
    But they're like strings in the fact that
    they can't be changed.
  • 2:45 - 2:49
    So here we create a three-tuple,
    a three-item tuple, and we try to change
  • 2:49 - 2:56
    the third thing from 3 to 0 and it says
    you can't do that, not mutable.
  • 2:56 - 2:56
    Okay?
  • 2:56 - 3:00
    So, so it's kind of like lists in
    the kind of data that we store them, we
  • 3:00 - 3:03
    store in them, and it's kind of like
    strings in that
  • 3:03 - 3:05
    you can't change them once you
    create them.
  • 3:05 - 3:09
    So this parentheses, this constant, is the
    moment of creation. Once
  • 3:09 - 3:12
    you've put the things in, you can't fiddle
    around with them.
  • 3:13 - 3:15
    There's a bunch of other things you can't
    do with tuples.
  • 3:15 - 3:18
    You think why am I even, why even use
    tuples?
  • 3:18 - 3:19
    We'll get to that in a second.
  • 3:19 - 3:21
    So here is a three-tuple
  • 3:21 - 3:23
    with the numbers 3, 2, 1.
  • 3:23 - 3:27
    You can't sort it, because if you
    sorted it, that would change it.
  • 3:27 - 3:29
    You can't add to it.
  • 3:29 - 3:32
    You can't append the value 5 to the end
    of it, because that would change it.
  • 3:32 - 3:36
    And you can't reverse it.
    So, none of these are allowed.
  • 3:38 - 3:43
    Those are things you can do with lists,
    but you can't do with tuples.
  • 3:43 - 3:44
    And you can read the documentation, but we
  • 3:44 - 3:47
    can also use that built-in dir function,
    that really
  • 3:47 - 3:50
    awesome dir function, where we make a list
    and we say hey, Python,
  • 3:50 - 3:53
    what will you let me do with lists?
  • 3:53 - 3:56
    Well, you can append, count, extend,
    index,
  • 3:56 - 3:59
    insert, sort, reverse, remove, pop. Lots
    of things.
  • 3:59 - 4:03
    Now we make a tuple and say hey, Python,
    what can we do with tuple?
  • 4:03 - 4:05
    Well, you can do a count or an index,
  • 4:05 - 4:07
    which means you can't do all these other
    things.
  • 4:07 - 4:10
    So this is sort of a, a very much a
    reduction.
  • 4:13 - 4:15
    because everything you can do with tuples,
    you can do with lists.
  • 4:15 - 4:18
    But not everything you can do with lists,
    you can do with tuples.
  • 4:18 - 4:19
    So why?
  • 4:19 - 4:23
    Why did I just waste all this time
    introducing tuples?
  • 4:23 - 4:24
    All they are is have parentheses.
  • 4:26 - 4:26
    What good are they?
  • 4:26 - 4:29
    Well, it turns out that they're much more
    efficient.
  • 4:29 - 4:31
    Because Python doesn't have to deal with
  • 4:31 - 4:34
    the fact that we, as programmers, might
    change them,
  • 4:34 - 4:38
    Python can make them quicker, they can use
    less memory,
  • 4:38 - 4:42
    all kinds of things that save a lot of
    processing time in Python.
  • 4:44 - 4:45
    So when would you use a tuple?
  • 4:45 - 4:46
    Well, in particular, if you're going to
  • 4:46 - 4:50
    create some list that you're never
    changing, we prefer to use tuples.
  • 4:50 - 4:52
    And there's a lot of situations in
  • 4:52 - 4:56
    programming where we create what we think
    of as a temporary variable.
  • 4:56 - 5:00
    And if we're going to use, create it, use
    it, and throw it
  • 5:00 - 5:05
    away without ever modifying it, we prefer
    tuples in those kinds of situations.
  • 5:05 - 5:09
    Okay? So we prefer tuples when we create
    things that are just temporary.
  • 5:09 - 5:11
    It's the fact that they're temporary
    variables.
  • 5:11 - 5:14
    They're like temporary lists. Because
    they're efficient.
  • 5:14 - 5:16
    They're quick to make and they're quick to
  • 5:16 - 5:17
    get rid of, and they're quick to go
    through.
  • 5:20 - 5:22
    Now, another really neat thing about
    Python that I
  • 5:22 - 5:25
    really like is that is the fact that you
    can
  • 5:25 - 5:28
    do sort of two assignments in one by putting
    a tuple on
  • 5:28 - 5:31
    both the left and the right-hand side of
    your assignment statement.
  • 5:31 - 5:33
    So if we think about an assignment
    statement,
  • 5:33 - 5:35
    I like to think of it as having a direction.
  • 5:35 - 5:37
    It says, these things go there.
  • 5:37 - 5:42
    Well, in Python, you can actually send two
    things at the same time.
  • 5:42 - 5:45
    The 4 goes into the x and the fred
    goes into the y.
  • 5:45 - 5:47
    This is a tuple.
    This is a tuple.
  • 5:47 - 5:50
    You, you cannot have constants on this
    left-hand side.
  • 5:50 - 5:53
    You can have variables or constants on
    the, or expressions
  • 5:53 - 5:57
    on the right-hand side, but this must be
    two variables.
  • 5:57 - 6:02
    Similarly, in this, the 99 goes into a and
    the 98 goes into b.
  • 6:02 - 6:04
    Now, it turns out that you can
  • 6:04 - 6:08
    syntactically eliminate the parentheses if
    you really want.
  • 6:08 - 6:10
    And so this leads to a prettier syntax,
  • 6:10 - 6:10
    I think.
  • 6:10 - 6:15
    It's the exact same thing with or without
    parentheses, where we basically just say,
  • 6:15 - 6:21
    hey, come back, a and b are assigned to
    the tuple 99, 98.
  • 6:21 - 6:23
    And so you can eliminate the parentheses
    as long
  • 6:23 - 6:26
    as it's very clear what's going on in the
    tuple.
  • 6:26 - 6:27
    And so this, this might be a little
    disquieting
  • 6:27 - 6:30
    when you first see it, but it's just a
  • 6:30 - 6:35
    tuple with no parentheses and the 99 goes
    to the a and the 98 goes to the b.
  • 6:35 - 6:38
    Now, it turns out we already did this.
  • 6:38 - 6:43
    I sort of blew by this in the previous lecture
    in dictionaries
  • 6:43 - 6:45
    because it allows us to go through the
  • 6:45 - 6:48
    dictionary's keys and values with two
    iteration variables.
  • 6:51 - 6:53
    And so, if you remember, here is a
    dictionary.
  • 6:53 - 7:02
    We put two items into it and and we can
    call d.items and get a list
  • 7:02 - 7:06
    of tuples, a list of two-tuples.
  • 7:06 - 7:10
    Two-tuples are a quick way of saying a
    tuple with two things in it.
  • 7:10 - 7:15
    It's a two-element list that consists each
    element is a two-tuple.
  • 7:15 - 7:17
    And it's the key and the value,
  • 7:17 - 7:21
    key and the value. And so if we
    just print this out, it's a list.
  • 7:21 - 7:26
    So then when we put this on a for loop, it
  • 7:26 - 7:32
    is a list, but the things inside the list
    are each a tuple.
  • 7:33 - 7:36
    Each thing inside the list is a tuple.
  • 7:37 - 7:41
    So, when this iteration variable goes to
    there,
  • 7:41 - 7:41
    it is like
  • 7:41 - 7:45
    this tuple is being assigned into k,v.
    Which means the key,
  • 7:45 - 7:49
    key goes into k and the value goes
    into v.
  • 7:49 - 7:52
    The name I picked for k and v don't
    matter, do not matter.
  • 7:53 - 7:55
    It's just, it's just the first the first
    one and the second one.
  • 7:57 - 8:02
    So k go, k and v point here.
    Then the next time through the loop, k
  • 8:02 - 8:06
    and v point here. And so that's
  • 8:06 - 8:11
    how csev 2 and Chen Wen 4 happen.
  • 8:11 - 8:15
    And so this is really a tuple assignment
    or a tuple iterating
  • 8:15 - 8:21
    through a list of tuple iteration
    variable or a pair of iteration variables
  • 8:21 - 8:23
    walking through the list, okay?
  • 8:25 - 8:29
    We don't do this a lot, and it's really
    quite, it's most heavily
  • 8:29 - 8:32
    used for this situation where you're going
    through a dictionary and you want to see
  • 8:32 - 8:34
    both the keys and the values.
  • 8:34 - 8:38
    And then you use this method inside of
    dictionary called d.items.
  • 8:39 - 8:41
    Another thing that's cool about tuples
  • 8:41 - 8:48
    are that they're comparable.
    So less than, greater than, equals.
  • 8:48 - 8:55
    And so, they look, they first compare
    the first, leftmost, thing, then
  • 8:55 - 8:58
    if that matches, they go to the second
    one, and then if that one matches,
  • 8:58 - 8:59
    they go to the third one.
  • 8:59 - 9:03
    And so if we're asking, is (0, 1, 2)
    less than (5, 1, 2).
  • 9:03 - 9:04
    And the answer is True.
  • 9:04 - 9:08
    And it only looks at the 0 and the
    5, and that's less than, so away we go.
  • 9:09 - 9:13
    If we ask is (0, 1, 2000000) less
    than (0, 3, 4)?
  • 9:13 - 9:17
    Well, 0 and 0 match, so it goes to
    the second one.
  • 9:17 - 9:20
    1 and 3, well they don't match and
    they're less than,
  • 9:20 - 9:23
    so 1 is less than 3, so it
  • 9:23 - 9:26
    so it's True and it doesn't even look at
    these numbers because it doesn't have to.
  • 9:26 - 9:26
    Right?
  • 9:26 - 9:28
    In this one, it doesn't look at those
    numbers.
  • 9:28 - 9:35
    And now if we say, come here, is Jones,
    Sally less than Jones, Fred?
  • 9:35 - 9:38
    Well, it compares this and they're equal.
  • 9:38 - 9:42
    So then it has to look to the second one:
    is Sally less than Fred?
  • 9:42 - 9:45
    Well, no, because S is not less than F.
  • 9:45 - 9:51
    And so that answer is False.
    Is Jones, Sally
  • 9:51 - 9:55
    greater than Adams, Sam? Well Jones is
    greater than Adams, so it
  • 9:55 - 9:58
    never looks at these variables, and that
    turns out to be True.
  • 9:59 - 10:02
    So these are comparable.
  • 10:02 - 10:05
    Which means we can use the less than, less
    than or equal to,
  • 10:05 - 10:09
    greater than or equal to, equal to, or
    not equal to.
  • 10:09 - 10:13
    So we can use these operators on whole
    tuples.
  • 10:13 - 10:15
    Now this turns out to be quite nice,
  • 10:15 - 10:20
    because things that can be compared can
    also be sorted.
  • 10:21 - 10:23
    Okay?
  • 10:23 - 10:24
    So here is
  • 10:24 - 10:28
    [COUGH] a, b, and c.
    a maps to 10.
  • 10:28 - 10:30
    b maps to 1.
    c maps to 22.
  • 10:30 - 10:33
    If I look at d.items,
  • 10:33 - 10:37
    I get back a list of two-tuples, three
    two-tuples.
  • 10:37 - 10:41
    They are not sorted because dictionaries
  • 10:41 - 10:43
    aren't sorted. a maps to 10,
  • 10:43 - 10:44
    c maps to 22,and b maps to 1.
  • 10:44 - 10:49
    The order that these come out in
    is not something that we can control.
  • 10:49 - 10:52
    But if we put these items into a variable,
  • 10:52 - 10:57
    call it t, t is the list of tuples
    basically,
  • 10:57 - 11:01
    and then we tell it to sort, it can do
    comparisons between all these.
  • 11:03 - 11:06
    And it can sort them and now they're
    sorted
  • 11:06 - 11:08
    in key order: a, b, c.
  • 11:08 - 11:10
    Now you'll never get any keys that match
  • 11:10 - 11:12
    so it never looks at the second one, right?
  • 11:12 - 11:16
    Because there's one and only one key a or
    b or c.
  • 11:16 - 11:20
    The value 10 never gets looked at.
    So this ends up sort by keys.
  • 11:20 - 11:28
    Sort by keys.
    Okay, so this is the way to sort by keys.
  • 11:29 - 11:31
    We take a dictionary.
  • 11:31 - 11:36
    We get back a list of tuples, key-value
    tuples, then we sort that dictionary.
  • 11:36 - 11:40
    I mean, sort that list of key-value tuples.
    And then, it's sorted by key.
  • 11:40 - 11:42
    Okay?
    So that's one sort.
  • 11:43 - 11:46
    There is a built-in function in Python
  • 11:49 - 11:53
    called sorted, which takes as a
    parameter a list,
  • 11:53 - 11:56
    and gives you back a sorted version of
    that list.
  • 11:56 - 12:00
    So we can collapse these operations by
    saying, oh,
  • 12:00 - 12:04
    well d sub items is this list of tuples
    non-sorted.
  • 12:04 - 12:09
    But sorted of d sub items is that same
    list of tuples, but then sorted.
  • 12:09 - 12:14
    So immediately in one step we have
  • 12:14 - 12:16
    a, b, and c properly sorted.
  • 12:16 - 12:20
    And we can combine into all this into one
    nice little for
  • 12:20 - 12:24
    statement, where we say for k, v in
    sorted of d sub items.
  • 12:24 - 12:29
    So this is now going to first sort the
    key-value pairs by key.
  • 12:29 - 12:32
    And then k, v is going to run through them,
    so k's
  • 12:32 - 12:36
    going to be a, 10. Then it's going to, k's
    going to be b,
  • 12:36 - 12:38
    v is going to be 1. k is going to be c,
  • 12:38 - 12:40
    v is going to be 22.
  • 12:40 - 12:45
    So now we've printed these things out in
    alphabetical key order.
  • 12:45 - 12:45
    Okay?
  • 12:45 - 12:48
    So by adding sorted to d.items, that
    means that
  • 12:48 - 12:52
    this loop is going to run in key-sorted
    order.
  • 12:54 - 12:55
    Key-sorted order.
  • 12:56 - 13:01
    And that's because sorted takes a list and
    then returns, as a,
  • 13:01 - 13:05
    takes a list as unsorted list as input and
    returns a sorted list.
  • 13:07 - 13:08
    Okay?
  • 13:10 - 13:15
    Now, if we are doing something like our
    common problem of what's the most common word,
  • 13:15 - 13:19
    what if we want to say, what's the five
    most common words?
  • 13:19 - 13:22
    In that case, we probably want to sort in
  • 13:22 - 13:26
    descending order by the values, not the
    key.
  • 13:27 - 13:27
    Okay?
  • 13:29 - 13:31
    So we want sort by the values instead of
    the key.
  • 13:32 - 13:37
    So this is a situation where we're
    going to create a temporary variable.
  • 13:37 - 13:40
    So here's how we're going to do it.
  • 13:40 - 13:44
    Here is our dictionary with a, 10 and we
    want to sort now by
  • 13:44 - 13:48
    the values, we want to, you know, maybe see
    the most common or sort by the values.
  • 13:48 - 13:51
    And so we are going to make a temporary
    list
  • 13:51 - 13:54
    and then we are going to loop through the
    items.
  • 13:54 - 13:59
    So, so this is going to just loop through
    them and it's
  • 13:59 - 14:02
    going to loop through them in non-sorted
    order and we are
  • 14:02 - 14:08
    going to add using the append operation to
    this little list that we are making.
  • 14:08 - 14:14
    But we're going to add a tuple that
    is value, comma, key.
  • 14:14 - 14:19
    So if we make the value first and the key
    second in this tuple.
  • 14:19 - 14:23
    So this syntax here, this parentheses v
    comma k.
  • 14:23 - 14:27
    that means make a two-tuple with values
    from the v and
  • 14:27 - 14:28
    k variable.
  • 14:28 - 14:33
    And, append a list. So you're going to end
    up with a list of two-tuples.
  • 14:35 - 14:40
    So if we, if we take a look when we're all
    done with this, each of these is a tuple.
  • 14:40 - 14:46
    10, a gets appended; 22, c gets appended,
    and it was simply the opposite order.
  • 14:46 - 14:51
    The, the tuple, each of the tuples now
    has the value first and the key second.
  • 14:51 - 14:52
    Value first, key second.
  • 14:52 - 14:54
    Value first, key second.
  • 14:54 - 14:57
    So this is a bit of temporary data that
  • 14:57 - 15:01
    we've created, this is a bit of temporary
    data that we've created.
  • 15:01 - 15:04
    Then what we do is we call the sort
    method.
  • 15:04 - 15:10
    Sort, take this list. Lists are mutable.
    The individual tuples can't be changed, but
  • 15:10 - 15:14
    the order of the tuples can be changed
    because they are in a list.
  • 15:14 - 15:18
    tmp.sort, and then we're going to say
    reverse equals True
  • 15:18 - 15:21
    so you sort from the highest down to the
    lowest. Okay?
  • 15:21 - 15:24
    And now, tmp has been sorted
  • 15:24 - 15:26
    and now it is in a new order.
  • 15:26 - 15:30
    22, 10, 1 is what caused it to be
    sorted.
  • 15:30 - 15:34
    So we know that the biggest value is 22,
    the key
  • 15:34 - 15:38
    of c. Next biggest is 10 with a key of a.
  • 15:38 - 15:42
    And the smallest is a key of 1, a value
    of 1 with a key of b.
  • 15:42 - 15:44
    So the trick here is
  • 15:44 - 15:47
    if we want to sort in some other
    way, we just construct
  • 15:47 - 15:50
    a list where we put it in the order that
    we want it sorted.
  • 15:50 - 15:52
    And this is more important now.
  • 15:52 - 15:57
    The value is more important than the key.
    Now if we had another,
  • 15:57 - 16:02
    like a 22, f, it would sort first on
    the 22.
  • 16:02 - 16:06
    And then it would, it would sort the f, 1
    after the c, 1.
  • 16:06 - 16:06
    Right?
  • 16:06 - 16:08
    So we don't have any duplicates.
  • 16:08 - 16:10
    But we could have the we could have the
    key of
  • 16:10 - 16:13
    c to 22 and we could have f also be 22.
  • 16:15 - 16:19
    Okay, so, take some time on this, get this
    one right.
  • 16:19 - 16:23
    So now I want to show you a program
  • 16:23 - 16:26
    that is going to show you the ten most
    common words.
  • 16:26 - 16:28
    We did a, a loop before
  • 16:31 - 16:33
    where we did the
  • 16:34 - 16:38
    most common word by doing a maximum loop
    at the end by looking
  • 16:38 - 16:42
    through all of the counts in a dictionary
    and then picking the maximum.
  • 16:42 - 16:44
    But what if you wanted the top ten?
  • 16:44 - 16:46
    Right, but that, that you don't want to
    write
  • 16:46 - 16:48
    a loop for that, so we're going to use
    sorting.
  • 16:48 - 16:51
    So here's what we're going to do.
    We're going to open a file.
  • 16:51 - 16:55
    We're going to create a empty counts
    dictionary.
  • 16:55 - 16:56
    Then we're going to
  • 16:56 - 17:01
    write a for loop that reads each
    line for line in fhand.
  • 17:01 - 17:05
    Then I'm going to split each line into
  • 17:05 - 17:09
    words, based on the spaces, using the dot
    split.
  • 17:09 - 17:13
    Then I'm going to loop through each word
    in each line
  • 17:13 - 17:18
    and use our histogram or dictionary
    pattern where
  • 17:18 - 17:21
    I say counts sub word equals counts dot get
  • 17:21 - 17:23
    word comma zero. That basically says
  • 17:23 - 17:25
    go look in counts.
  • 17:25 - 17:27
    If the word key exists, give me back
  • 17:27 - 17:30
    the value that's in that, otherwise give
    me zero.
  • 17:30 - 17:31
    So this both
  • 17:31 - 17:35
    creates the new entries and updates old
    entries.
  • 17:35 - 17:38
    All in one nice simple statement.
  • 17:38 - 17:42
    So at the end of this bit of code
    right here
  • 17:42 - 17:48
    we are going to have counts with keyword
    word-count pairs.
  • 17:48 - 17:51
    Okay?
    So, this is something we've done before.
  • 17:51 - 17:55
    It's just dictionaries, reading, splitting.
  • 17:55 - 17:59
    And then this pattern of how to accumulate
    in a dictionary.
  • 17:59 - 18:03
    Then what we're going to do is we are going
    to make a new list
  • 18:03 - 18:07
    called l-s-t and now we're doing this
    key-value in the items.
  • 18:07 - 18:12
    So this is going to go through the
    key-value pairs in this list, which is the
  • 18:12 - 18:15
    key-value pairs from the dictionary.
    Right?
  • 18:16 - 18:19
    But then we are going to create this
    temporary list
  • 18:19 - 18:22
    of tuples that are val, comma, key.
  • 18:22 - 18:28
    So val is like 20, the; 14, hello;
  • 18:29 - 18:33
    and that's what the list is going to
    look like, right?
  • 18:33 - 18:34
    It's going to be tuples,
  • 18:34 - 18:37
    but it's going to be the value and then the
  • 18:37 - 18:39
    key, rather than the key and the value.
  • 18:39 - 18:47
    This one here is key, value; this one
    here, l-s-t, is value, key.
  • 18:47 - 18:51
    Now that we have a list that's value, comma,
    key,
  • 18:53 - 18:56
    we are just going to sort it because now
    it's going to sort based on the first
  • 18:56 - 18:59
    thing in that tuple and we're going to
    reverse it
  • 18:59 - 19:02
    so the biggest values are near the top.
  • 19:02 - 19:05
    And so when we're all done this is going
    to be
  • 19:05 - 19:09
    a list, except it's going to be sorted
    based on the value.
  • 19:09 - 19:11
    So that's just one step to sort it.
  • 19:11 - 19:15
    So this is a good example of how we sort
    of go through some work, we get a data
  • 19:15 - 19:17
    structure, a list, the way we want it and
  • 19:17 - 19:19
    now we can sort of leverage the built-in
    sort.
  • 19:19 - 19:23
    We had to prepare a list so we could use the
    built-in sort.
  • 19:23 - 19:25
    We could do this by hand, but it'd be very
    difficult.
  • 19:25 - 19:27
    But it's easier to say I think
  • 19:27 - 19:29
    I'll make a list, and then I'll sort it.
  • 19:29 - 19:30
    Okay?
  • 19:30 - 19:32
    So I, you know, I made two lists
    basically.
  • 19:32 - 19:37
    I made the original one, then I made this
    one just for the purpose of sorting.
  • 19:37 - 19:40
    And now what I am going to do to print out
    the top ten
  • 19:40 - 19:44
    is I am going to write a for loop val, key.
  • 19:44 - 19:48
    Remember, this list l-s-t is value-key.
  • 19:48 - 19:52
    And I'm going to say for val, key in lst,
    using list slicing,
  • 19:54 - 19:56
    starting at zero, up to but not including
  • 19:56 - 19:59
    ten, which is indeed is the first ten
    items.
  • 20:00 - 20:07
    Now I'm going to print out key, value, so
    it's going to print out the, 22;
  • 20:07 - 20:12
    fred, 16; and so I'm going to first print
    the first ten.
  • 20:12 - 20:16
    So, this list is in val-key order, the
    tuples are val-key order.
  • 20:16 - 20:19
    And so I'm going to print it out in
    key-val just so that I print out in a way
  • 20:19 - 20:21
    that makes the most sense.
  • 20:21 - 20:24
    And so, this is a simple way to do a
  • 20:24 - 20:27
    simple histogram of the occurrence of words
    in a file.
  • 20:29 - 20:34
    So again, you should know this, you should
    know every line.
  • 20:34 - 20:38
    You should know every line.
  • 20:38 - 20:41
    Go back, review a couple times, but you
    should know,
  • 20:41 - 20:43
    you should know the meaning of every line
    of this.
  • 20:43 - 20:46
    And if you do, that's really good. So
  • 20:50 - 20:54
    as you become more powerful and capable
    inside Python, you will
  • 20:54 - 20:58
    realize that there are sometimes even
    shorter ways of doing things.
  • 20:58 - 21:02
    Now, what I'm showing you here is not that
    different than what was
  • 21:02 - 21:06
    on the previous page, it's just really
    dense, but you have to concentrate.
  • 21:06 - 21:10
    So if, I want you to understand what's on
    that previous page.
  • 21:10 - 21:11
    If you don't understand this, don't feel bad.
  • 21:11 - 21:15
    I am going to explain it to you but don't
    feel bad if you don't get it.
  • 21:15 - 21:15
    Okay?
  • 21:15 - 21:16
    So I'm just going to explain it.
  • 21:18 - 21:22
    If it doesn't feel right to you, go back
    and look at the previous page.
  • 21:22 - 21:23
    Okay.
  • 21:23 - 21:27
    So here we go.
    I am going to have a dictionary.
  • 21:27 - 21:33
    And then I'm going to print, in one line,
    sorted by value.
  • 21:33 - 21:37
    So we'll start from the inside out.
  • 21:37 - 21:41
    So this is a thing called list
    comprehension.
  • 21:41 - 21:44
    It looks like a list constant because we
    start with square brackets.
  • 21:44 - 21:50
    But this is a Python syntax that says
    construct dynamically
  • 21:50 - 21:56
    a list of tuples v, comma, k and I would like
  • 21:56 - 22:02
    you to loop through the items with k and v
    taking on the successive values.
  • 22:02 - 22:06
    So this is creating that reversed list
    where value and key
  • 22:06 - 22:10
    are the order of the items in each tuple.
  • 22:10 - 22:12
    And it's going to do that, so this is
    going to expand.
  • 22:12 - 22:14
    It's sort of like, it goes [SOUND]
    expands this,
  • 22:14 - 22:17
    it makes a temporary list, right now.
  • 22:17 - 22:21
    Now if you look on the previous slide we
    call that thing l-s-t.
  • 22:21 - 22:27
    But here we don't even call it l-s-t, and
    then once we have the list of tuples
  • 22:27 - 22:34
    in value-key order, then we simply take
    and pass that into sorted.
  • 22:34 - 22:36
    This is a function call,
  • 22:36 - 22:41
    the sorted function. And then, now I'm not
    reversing it, but the print statement prints
  • 22:41 - 22:47
    out its ascending order of the value
    1, 10, 22.
  • 22:47 - 22:50
    Okay? So this you can, you can make these
    more
  • 22:50 - 22:54
    dense once you're a little more
    comfortable with what's going on.
  • 22:54 - 22:58
    It's sometimes easier to construct
    something that seems to have
  • 22:58 - 23:00
    steps, where you can put, you know,
    you can put
  • 23:00 - 23:01
    a debug print here,
  • 23:01 - 23:03
    you can put a debug print here, you can
    do a debug print here,
  • 23:03 - 23:06
    and you kind of see what's
    going on, right?
  • 23:06 - 23:08
    Whereas once you really understand this
  • 23:08 - 23:12
    you can, you can write some more dense
    Python.
  • 23:12 - 23:15
    When you understand this, it's okay.
  • 23:15 - 23:15
    Right?
  • 23:15 - 23:18
    So I'm not saying you're supposed to
    understand this, but I just want
  • 23:18 - 23:21
    to point out that it's possible to do this
    in a tighter fashion.
  • 23:23 - 23:23
    So,
  • 23:25 - 23:29
    tuples are like lists except that you
    can't change them.
  • 23:29 - 23:30
    Right?
  • 23:30 - 23:35
    You can't change lists. And now you can
    compare them, you can sort them.
  • 23:35 - 23:39
    You can sort lists of tuples.
    You can't sort within the tuple itself.
  • 23:39 - 23:44
    The two values on the left-hand side of
    the assignment statement, we can
  • 23:45 - 23:49
    use sorted, and we played with sorting
    dictionaries by key and value.
  • 23:49 - 23:50
    So,
  • 23:52 - 23:54
    that's kind of the end of this lecture.
  • 23:54 - 23:57
    And and so at this point I just want to
    kind of congratulate
  • 23:57 - 24:02
    you on making it through the first ten
    chapters of the book.
  • 24:02 - 24:05
    So I'll, I'll drink a cup of tea to you.
  • 24:05 - 24:08
    Here's your cup of tea, here's my toast to
    you.
  • 24:08 - 24:11
    in my Slitherin cup.
  • 24:11 - 24:16
    And so it's time for a graduation
    ceremony.
  • 24:16 - 24:17
    So, I'll give a
  • 24:17 - 24:22
    a little graduation speech here with my
    graduation hat on and this is my
  • 24:22 - 24:27
    this is my Slitherin wand and so, so the
    reason I am congratulating you at
  • 24:27 - 24:32
    the end of this chapter is that
    at this point, you kind of
  • 24:34 - 24:39
    know almost, you know all
    the fundamentals of programming.
  • 24:39 - 24:44
    Programming really comes down to what's
    called algorithms and data structures.
  • 24:44 - 24:50
    Sometimes we solve a problem by a clever
    series of steps that we put together.
  • 24:50 - 24:54
    And sometimes we solve a problem by
    creating a clever data structure.
  • 24:56 - 24:59
    And so the first few chapters were about
    algorithms. Steps,
  • 24:59 - 25:04
    loops, functions, very procedural. How you
    sort of create these threads of
  • 25:04 - 25:09
    stepping and do things a bunch of times or
    skip around or whatever.
  • 25:09 - 25:11
    And in the last three chapters that
    we've covered
  • 25:11 - 25:15
    we're talking about data structures. And
    programming
  • 25:15 - 25:19
    power comes when you combine algorithms
    and data structures.
  • 25:20 - 25:25
    Now in the next chapters, starting with
    Chapter Eleven, regular expressions,
  • 25:25 - 25:29
    we're going to learn sort of more clever
    ways of doing the same thing.
  • 25:29 - 25:32
    So you kind of know how to do a lot of
    stuff now.
  • 25:32 - 25:35
    From this point forward, you'll see, oh,
    boy, that's more clever.
  • 25:35 - 25:38
    Or we'll use a database.
    Oh, that's more clever.
  • 25:38 - 25:44
    But it's not fundamentally different. And
    so that's why it's important for you
  • 25:44 - 25:50
    to understand, before you leave this
    moment, to understand everything
  • 25:50 - 25:51
    that we've covered so far.
  • 25:51 - 25:56
    Loops, functions, strings, files,
  • 25:56 - 26:02
    tuples, lists, dictionaries, because they
    are kind of the foundation and everything
  • 26:02 - 26:07
    else will just kind of be a subtle
    refinement/improvement.
  • 26:07 - 26:10
    So once you understand that you've kind of
    begun, you've
  • 26:10 - 26:13
    become a basic programmer and I like, I like
    poof!
  • 26:13 - 26:15
    Like I, I like
  • 26:15 - 26:20
    magically asperio you and turn you into
    Pythonio, something like that.
  • 26:20 - 26:20
    Okay.
  • 26:20 - 26:23
    Enough with the Harry Potter reference.
  • 26:23 - 26:26
    Thank you for spending all this time with
    me.
  • 26:26 - 26:28
    If you've gotten this far, I really
    appreciate it.
  • 26:30 - 26:32
    And of course it's really just the
    beginning but
  • 26:32 - 26:36
    I hope that it has been a good beginning.
  • 26:36 - 26:36
    Thank you.
Title:
Python for Informatics - Chapter 10 - Tuples
Description:

This is Chapter 10 from Python for Informatics - Exploring Information. From www.pythonlearn.com.
All Lectures: http://www.youtube.com/playlist?list=PLlRFEj9H3Oj4JXIwMwN1_s­s1Tk8wZShEJ

more » « less
Video Language:
English
Team:
Captions Requested
Duration:
26:37

English subtitles

Revisions