Return to Video

Python for Informatics - Chapter 9 - Dictionaries

  • 0:00 - 0:05
    Hello again, and welcome to Chapter Nine
    of Python, Dictionaries.
  • 0:05 - 0:09
    As always, this lecture is copyright
    Creative Commons Attribution.
  • 0:09 - 0:14
    That means the audio, the video, the
    slides, and even my scribbles.
  • 0:14 - 0:18
    You can use them any way you like, as long
    as you attribute them.
  • 0:18 - 0:20
    Okay, so this is the second chapter
  • 0:20 - 0:22
    where we're talking about collections, and
    the collections
  • 0:22 - 0:26
    are like a piece of luggage in that you
    can put multiple things in them.
  • 0:28 - 0:30
    Variables that we've talked about sort of
    starting in
  • 0:30 - 0:35
    Chapter Two and Chapter Three were simple
    variables, scalar.
  • 0:35 - 0:37
    They're just kind of one thing, and as
    soon as you,
  • 0:37 - 0:41
    like, put another thing in there, it
    overwrites the first thing.
  • 0:41 - 0:46
    And so if you look at the code, you know,
    x = 2 and x = 4,
  • 0:46 - 0:51
    the question is, you know, where did
    the 2 go?
  • 0:51 - 0:53
    Right? The 2 was there, x was there,
  • 0:53 - 0:57
    there was a 2 in there, and then we cross
    it out and put 4 in there.
  • 0:57 - 1:01
    This is sort of the basic operation, the
    assignment statement, it's a replacement.
  • 1:01 - 1:04
    But a dictionary allows us to put more
    than one thing.
  • 1:04 - 1:06
    Not using this syntax, but it allows us to
  • 1:06 - 1:09
    have a variable that's really an aggregate
    of many values.
  • 1:09 - 1:12
    And the difference between a list and a
    dictionary
  • 1:12 - 1:16
    is how the values are structured within
    that single variable.
  • 1:16 - 1:18
    The list is a linear collection,
  • 1:18 - 1:21
    indexed by integers 0, 1, 2, 3.
  • 1:21 - 1:24
    If there's five of them, it's 0 through 4,
    very much like a
  • 1:24 - 1:28
    Pringle's can here, where they're just
    stacked nicely on top of each other.
  • 1:28 - 1:33
    Everything's kind of organized. We talked
    about it in the last, in the last lecture.
  • 1:33 - 1:36
    This lecture we're talking about dictionaries.
  • 1:36 - 1:38
    A dictionary's very powerful.
  • 1:38 - 1:42
    It's, and its power comes from a different
    way of organizing itself internally.
  • 1:42 - 1:44
    It's a bag of values,
  • 1:44 - 1:48
    like a just sort of, just stuff's
    in it, it's not in any order.
  • 1:48 - 1:49
    Big stuff, little stuff.
  • 1:49 - 1:51
    Things have labels.
  • 1:51 - 1:52
    You can also think of it like a purse with
  • 1:52 - 1:55
    just things in it that's like, it's not
    like stacked.
  • 1:55 - 1:58
    It's just, stuff moves around as you're going
  • 1:58 - 2:01
    and that's, that's a very good model for
    dictionaries.
  • 2:02 - 2:03
    And so dictionaries
  • 2:03 - 2:06
    have to have a label because the stuff is
    not in order.
  • 2:06 - 2:08
    There's no such thing as the third thing.
  • 2:08 - 2:10
    There is the thing with the label perfume.
  • 2:10 - 2:11
    There's the thing with the label candy.
  • 2:11 - 2:14
    There's the thing with the label money.
  • 2:14 - 2:17
    And so there's the value, the thing, the money.
  • 2:17 - 2:19
    And then there's always also the label.
  • 2:19 - 2:23
    We also call these key/value.
  • 2:25 - 2:29
    The key is the label and the value is
    whatever.
  • 2:29 - 2:31
    And so these pink things are all labels for
  • 2:31 - 2:33
    various things you could put in your purse.
  • 2:33 - 2:36
    So you could say to your purse, "hey purse,
    give me my tissues."
  • 2:36 - 2:38
    "Hey purse, give me my money."
  • 2:38 - 2:40
    And it, it's in there somewhere and the
    purse sort of
  • 2:40 - 2:43
    gives you back the tissues or the money.
  • 2:43 - 2:49
    And it's, Python's most powerful data
    collection is the dictionaries.
  • 2:49 - 2:50
    And it's when
  • 2:50 - 2:52
    you get used to wielding them you'll say,
    like,
  • 2:52 - 2:54
    whoa, I can do so much with these things.
  • 2:54 - 2:56
    And at the beginning you just sort of
  • 2:56 - 3:00
    learning sort of how to use them without
    hurting yourself.
  • 3:00 - 3:01
    But they're very powerful.
  • 3:01 - 3:02
    It's like a database.
  • 3:02 - 3:07
    It's, it allows you to store very arbitrary
    data organized in however you feel like
  • 3:07 - 3:11
    organizing it, in a way that advances the
    cause of the program that you're writing.
  • 3:11 - 3:16
    And we're still kind of at the very
    beginning, but as you learn more,
  • 3:16 - 3:18
    these will become a very powerful
    tool for you.
  • 3:20 - 3:23
    They, dictionaries have different names in
    different languages.
  • 3:25 - 3:27
    PERL or PHP would call them associative
    arrays.
  • 3:29 - 3:32
    Java would call them a PropertyMap or a
    HashMap.
  • 3:32 - 3:35
    And C# might call them a property bag or
    an attribute bag.
  • 3:35 - 3:38
    And so they're, they're just the same
    concept.
  • 3:38 - 3:42
    It's keys and values is the concept that's
    across all these languages.
  • 3:42 - 3:44
    Just are very powerful.
  • 3:44 - 3:45
    And if you look at the Wikipedia entry
  • 3:45 - 3:46
    that I have here
  • 3:46 - 3:49
    you can see that it's just, it's a concept
  • 3:49 - 3:53
    that we give different names in different
    languages. Same concept, different names.
  • 3:54 - 3:58
    So like I said, the difference between a
    list and a dictionary, they both can store
  • 3:58 - 4:01
    multiple values. The question is how we
    label them,
  • 4:01 - 4:03
    how we store them, and how we retrieve
    them.
  • 4:03 - 4:07
    So here's an example use of a dictionary.
    I'm going to make a thing called purse.
  • 4:07 - 4:11
    And I'm going to store in purse, this is
    an assignment statement,
  • 4:11 - 4:14
    purse sub money.
    So this isn't like sub zero.
  • 4:14 - 4:15
    This is sub money.
  • 4:15 - 4:18
    So I'm actually using a string as the
    place.
  • 4:18 - 4:21
    And, so I'm going to say stick 12
    in my purse
  • 4:21 - 4:24
    and stick a Post-it note that says
    that's my money.
  • 4:24 - 4:26
    Candy is 3. Tissues is 75.
  • 4:26 - 4:32
    And if I look at that, it's not just the
    numbers 12, 3, and 75 as it
  • 4:32 - 4:37
    would be in a list. It is the connection
    between money and 12,
  • 4:37 - 4:42
    tissues is 75, candy is 3.
    And in the key/value, that's the
  • 4:42 - 4:47
    key and that's the value.
    So candy is the key and 3 is the value.
  • 4:47 - 4:52
    Now I can look things up by their name,
    print purse sub candy.
  • 4:52 - 4:57
    Well it goes and finds it, asking hey purse,
    give me back candy, and it
  • 4:57 - 5:00
    goes and finds the value, which is 3, and
    so out comes a 3.
  • 5:00 - 5:03
    We can also put it
  • 5:03 - 5:06
    on the right-hand side of an
    assignment statement,
  • 5:06 - 5:07
    so purse sub candy says give me
    the old version of candy,
  • 5:07 - 5:10
    and then add 2 to it, which
  • 5:10 - 5:14
    gives me 5, and then store it back
    in that purse
  • 5:14 - 5:16
    under the label candy.
  • 5:16 - 5:19
    So we see candy changing to 5.
  • 5:19 - 5:21
    And so, this is a place, and you could
  • 5:21 - 5:23
    do this with a list except these would be
    numbers.
  • 5:23 - 5:28
    You could say purse sub two is equal to
    purse sub two plus two, or whatever.
  • 5:28 - 5:32
    But in dictionaries, there are labels.
  • 5:32 - 5:33
    Now, they're not strings.
  • 5:33 - 5:35
    Strings is a very common label in
    dictionaries, but
  • 5:35 - 5:38
    it's not always strings, you can use other
    things.
  • 5:38 - 5:40
    In this chapter we'll pretty much focus on
    strings.
  • 5:40 - 5:44
    You can even use numbers and then you
    would get a little confused.
  • 5:44 - 5:45
    But you can.
  • 5:45 - 5:48
    So here's sort of a picture of how this
    works.
  • 5:48 - 5:53
    So, if we take a look at this line purse
    sub money equals 12,
  • 5:53 - 5:58
    it's like we were putting a key/value
    connection, money is the label for 12.
  • 5:58 - 6:01
    And then we sort of move that in.
  • 6:01 - 6:04
    And it's up to the purse to decide
    where things live.
  • 6:04 - 6:10
    If we look at the next line, we're going to
    put the value in with a
  • 6:10 - 6:12
    3 in with the label candy, and we're
    going to put
  • 6:12 - 6:15
    the value 75 in with the label of tissues.
  • 6:15 - 6:18
    And when we say hey purse, print yourself
    out, it just
  • 6:18 - 6:21
    goes and pulls these things back out and
    hands them to us.
  • 6:21 - 6:25
    And what it's really, it's giving us both the
    label and the value and it's necessary
  • 6:25 - 6:26
    to do that cause they're just like 12,
  • 6:26 - 6:29
    75, and 3. What exactly is that?
  • 6:29 - 6:31
    And so this syntax with the curly braces
  • 6:31 - 6:35
    is what happens when you print a
    dictionary out.
  • 6:35 - 6:39
    The same thing happens when we're sort of
    printing purse sub candy, right?
  • 6:39 - 6:40
    Purse sub candy,
  • 6:42 - 6:45
    it's like dear purse, go and find the candy
    thing.
  • 6:45 - 6:46
    Look at that one, look at that one.
  • 6:46 - 6:48
    Oh, yep, yep, this is candy.
  • 6:48 - 6:50
    But what we're looking for is the value,
  • 6:50 - 6:53
    and so that's why 3 is coming out here.
  • 6:53 - 6:57
    So go look up under candy, and tell me
    what's stored under candy.
  • 6:57 - 6:59
    These can be actually more complex things,
  • 6:59 - 7:01
    I'm just keeping it simple for this
    lecture.
  • 7:03 - 7:08
    And then, when we say purse sub candy
    equals purse sub candy plus 2, well it
  • 7:08 - 7:14
    pulls the 3 out, looking at the label
    candy, then adds 3 plus 2 and makes 5,
  • 7:14 - 7:20
    and then it assigns it back in, and then
    that says, oh, go, go place this number 5
  • 7:20 - 7:26
    in the purse with the label of candy,
    which then replaces the 3 with a 5.
  • 7:26 - 7:27
    Okay?
  • 7:28 - 7:30
    And if we print it out, we see that the
  • 7:30 - 7:35
    new variable, or the new candy entry,
    is now 5.
  • 7:35 - 7:36
    Okay?
  • 7:37 - 7:41
    So if we just sort of put these things
    side by side, we create
  • 7:41 - 7:44
    them sort of both the same way and we make
    an empty list, and an empty
  • 7:44 - 7:47
    dictionary, we call the append method
    because
  • 7:47 - 7:49
    we're sort of just putting these things in
  • 7:49 - 7:52
    order. You gotta put the first one in
    first. So it's not telling you where.
  • 7:52 - 7:53
    You kind of know that this
  • 7:53 - 7:55
    will be the first one, cause we're
    starting with an empty one,
  • 7:55 - 7:57
    and this will be the second one.
  • 7:57 - 8:02
    We put in the values 21 and 183, and then
    we print it out, and it's like okay, you gave
  • 8:02 - 8:04
    me the values 21 and 183, I will maintain
    the order for you,
  • 8:04 - 8:08
    there's no keys other than their position.
  • 8:08 - 8:12
    The position is the key, as it were, so if
    I want to to change the first one to 23,
  • 8:12 - 8:17
    well, I say list sub zero, which is this,
    and then change that to 23.
  • 8:17 - 8:20
    So this is sort of used as a lookup to
  • 8:20 - 8:23
    find something. It can be used on either the
    right-hand side or the
  • 8:23 - 8:25
    left-hand side of an assignment statement.
  • 8:25 - 8:28
    Comparing that to dictionaries, I want to
    put a 21 in there
  • 8:28 - 8:30
    and I want to put it with the label age.
  • 8:30 - 8:33
    I'm going to put 182, put that in with the
    label course.
  • 8:33 - 8:37
    So we don't have to like, make an entry.
  • 8:37 - 8:38
    The fact that the entry doesn't exist,
  • 8:38 - 8:42
    it creates the age entry and sticks 21 into it,
  • 8:42 - 8:44
    creates the course entry, sticks 182 into it.
  • 8:44 - 8:49
    We print it out and it says, oh, course
    is 182 and age is 21.
  • 8:49 - 8:55
    This emphasizes that order is not
    preserved in dictionaries.
  • 8:56 - 8:58
    I won't go into like great detail as to
    why that is.
  • 8:58 - 9:01
    It turns out that that's a compromise that
  • 9:01 - 9:05
    makes them fast using a technique called
    hashing.
  • 9:05 - 9:09
    It's how it actually works internally,
    go Wikipedia hashing and
  • 9:09 - 9:10
    take a look.
  • 9:10 - 9:14
    But, the thing that matters to us as
    programmers primarily
  • 9:14 - 9:20
    is that lists maintain order and
    dictionaries do not maintain order.
  • 9:20 - 9:24
    They, dictionaries give us power
    that we don't have in lists.
  • 9:24 - 9:26
    I mean they're very complimentary.
  • 9:26 - 9:28
    Now there's not this one that's better
    than the other.
  • 9:28 - 9:29
    They've very complimentary.
  • 9:29 - 9:32
    Different kinds of data is either better
    represented as a list
  • 9:32 - 9:33
    or as a dictionary, depending on the
  • 9:33 - 9:35
    problem you're trying to solve.
  • 9:35 - 9:39
    And in a moment we'll, we'll be writing
    programs that are using both.
  • 9:39 - 9:41
    So if we come down here and I say,
  • 9:41 - 9:47
    okay, stick 23 into, assignment statement,
    into ddd sub age,
  • 9:47 - 9:51
    well that will change this 21 to 23,
    so when we print it out.
  • 9:51 - 9:53
    So you can, this part, where you look
    something up and
  • 9:53 - 9:56
    change the value, you can do either way.
  • 9:56 - 9:58
    It's just how you do it here
  • 9:58 - 10:00
    is a little bit different, okay?
  • 10:00 - 10:04
    So let's look through this code again.
  • 10:04 - 10:07
    And so I like, I like to use the word key
    and value.
  • 10:07 - 10:09
    Key is the way we look the thing up,
    and in lists
  • 10:09 - 10:13
    keys are numbers starting at
    zero and with no gaps.
  • 10:13 - 10:15
    In dictionaries keys are whatever we want
    them to be,
  • 10:15 - 10:18
    in this case I'm using strings.
  • 10:18 - 10:21
    And then the value is the number we're
    storing in it.
  • 10:21 - 10:25
    So we create this kind of a list with that
    kind, those
  • 10:25 - 10:26
    kinds of statements.
  • 10:26 - 10:29
    This statement creates this kind of a thing.
  • 10:29 - 10:34
    Now, if we, if we think of this assignment
    statement as moving data
  • 10:34 - 10:37
    into a new, into a place, a new item of
    data into a place.
  • 10:41 - 10:43
    It's looking at this thing right here.
  • 10:43 - 10:45
    Right? It's like, that's where I want to
    move it.
  • 10:45 - 10:48
    And so it hunts, and says, look the key up.
  • 10:48 - 10:50
    And that's the one that I'm going to change.
  • 10:50 - 10:52
    And then once it knows which it's going to
    change,
  • 10:52 - 10:57
    then it's going to take the 23, and it's
    going to put the 23 into that location.
  • 10:57 - 11:01
    And so that's how this changes from that
    to that.
  • 11:01 - 11:07
    Similarly when we get down to here, we're
    going to stick 23 somewhere and
  • 11:07 - 11:10
    this is, this expression, this lookup
    expression, the index
  • 11:10 - 11:13
    expression ddd sub age, is where we're
    going to put it.
  • 11:13 - 11:16
    So, we're looking here, where is that thing?
  • 11:16 - 11:20
    Well, that thing is this entry
  • 11:20 - 11:23
    in the dictionary. And so now when we're
    going to store the 23,
  • 11:23 - 11:24
    we know where the 23 is going to go.
  • 11:24 - 11:27
    It's going to overwrite the 21 and so the
    21 is
  • 11:27 - 11:31
    going to change to 23, okay? So they're
    kind of similar.
  • 11:31 - 11:34
    There are things that work similar in them
  • 11:34 - 11:36
    and then there are things that work
    differently in them.
  • 11:38 - 11:41
    We can make literals, constants, with
  • 11:41 - 11:43
    curly braces. And they look just like the print.
  • 11:43 - 11:45
    That's one nice thing about Python.
  • 11:45 - 11:49
    When you print something out it's showing
    you how you can make a literal, and
  • 11:49 - 11:56
    basically you just open with a curly brace
    and say chuck colon 1, fred 42, jan 100.
  • 11:56 - 11:58
    And we're making connections.
  • 11:58 - 12:02
    key/value pair, key/value pair.
    We print it out and
  • 12:05 - 12:06
    No order. They don't maintain order.
  • 12:06 - 12:09
    Now they might come out in the same order,
    but that's just lucky.
  • 12:09 - 12:09
    Right?
  • 12:09 - 12:11
    All the ones I've shown you so far don't
  • 12:11 - 12:13
    come out in the same order, which is good
    to demonstrate it.
  • 12:13 - 12:16
    If it one time came out in the same order
    that wouldn't be broken.
  • 12:16 - 12:18
    It's not like it doesn't want to come out
    in the same order.
  • 12:18 - 12:22
    It's just, you don't, it's not internally
    stored, and you
  • 12:22 - 12:24
    add an element and it may reorder them.
  • 12:25 - 12:28
    You can do an empty dictionary with just a
    curly brace, curly brace.
  • 12:33 - 12:37
    So, I'm going give you another example.
  • 12:37 - 12:40
    And I'm going to show you a series of
    names.
  • 12:40 - 12:46
    And I want you to figure out what the most
    common name is
  • 12:46 - 12:48
    and how many times each name appears.
  • 12:48 - 12:52
    Now these are real people.
    They actually work on the Sakai project.
  • 12:52 - 12:59
    Steven, Zhen, and Chen, and me.
    So these are people that are actually
  • 12:59 - 13:01
    in the data that we use in this course.
  • 13:01 - 13:04
    Okay? And so I think I'll show you about
    fifteen names
  • 13:04 - 13:07
    and you're to come up with a way, I'm
    going to
  • 13:07 - 13:11
    show them to you one at a time, you need to
    come up with a way to keep track of these.
  • 13:11 - 13:12
    Okay?
  • 13:12 - 13:16
    So I'll just, with no further ado I will show
    you the names.
  • 13:16 - 13:26
    [BLANK_AUDIO]
  • 13:54 - 13:58
    Okay, so that's all the names.
    Did you get it?
  • 13:58 - 14:00
    You might have to go back and do it again.
  • 14:01 - 14:04
    How did you solve the problem?
  • 14:04 - 14:08
    What kind of a data structure did you
    build to solve the problem?
  • 14:08 - 14:11
    Or did you just say wow that's painful, I
  • 14:11 - 14:15
    think I will learn Python instead, in
    solving that problem.
  • 14:15 - 14:16
    Okay?
  • 14:16 - 14:20
    So pause the, pause the video if you want and
  • 14:20 - 14:23
    write down or go back, write down what you
    think the
  • 14:23 - 14:28
    number of the most common name is and how
    many times.
  • 14:30 - 14:32
    Okay. Now I'll show you.
  • 14:32 - 14:35
    So here is the whole list.
    It's all of them.
  • 14:35 - 14:39
    And now that we see all of them, we
    use our amazing human
  • 14:39 - 14:43
    mind and we scan around, and look at
    purpleness and, and all that stuff.
  • 14:43 - 14:44
    And then we go like, oh, this is a so
  • 14:44 - 14:46
    much easier problem when I'm looking
    at the whole thing.
  • 14:48 - 14:52
    And I think that the most common person is
    Zhen, and
  • 14:54 - 14:59
    I think we see Zhen, I think we see Zhen
    five times.
  • 15:01 - 15:07
    And I think csev is one, two, three and
    Chen Wen is one, two.
  • 15:07 - 15:09
    And Steve Marquard is one, two, three.
  • 15:09 - 15:13
    So the question is, what is an effective
    data structure if you going to see
  • 15:13 - 15:16
    a million of these, what kind of data
    structure would you have to produce?
  • 15:16 - 15:17
    Because you can't keep it in you head
  • 15:17 - 15:20
    even, even this number of people, you can't
  • 15:20 - 15:22
    even this amount of data, no way you can
    keep it in your head. You have to come
  • 15:22 - 15:25
    up with some kind of a variable, as it were,
  • 15:25 - 15:28
    just like largest so far was the variable.
  • 15:28 - 15:30
    Some kind of variable that gets you to
  • 15:30 - 15:31
    the point where you understand what's
    going on.
  • 15:31 - 15:35
    And so this is the most common technique
    to solve this
  • 15:35 - 15:39
    problem where you keep a running total of
    each of the names.
  • 15:39 - 15:42
    And if you see a new name, you add them to
    the list.
  • 15:42 - 15:45
    So csev and then you give him a one,
  • 15:45 - 15:47
    and then you see Zhen and you give her a
    one,
  • 15:47 - 15:50
    and then you see Chen and you give her a
    one.
  • 15:50 - 15:52
    And then you see csev again and you give
    him a two.
  • 15:52 - 15:55
    And you see a two, and a two, and a one
    right?
  • 15:55 - 15:57
    [COUGH]
  • 15:57 - 16:03
    And so then when you're all done you have
    the mapping, right, of these things
  • 16:03 - 16:06
    and you go oh, okay, let me look through
    here and find the largest one.
  • 16:06 - 16:10
    That's the largest one and so that must be
    the person who is the most.
  • 16:10 - 16:12
    So you need a scratch area,
  • 16:12 - 16:15
    a data structure or a piece of paper as
    it were,
  • 16:15 - 16:19
    and so that's what, exactly what
    dictionaries are really good at.
  • 16:19 - 16:24
    You could think of this as like a
    histogram. You know, it's,
  • 16:24 - 16:28
    it's a bunch of counters, but counters
    that are indexed by a string.
  • 16:28 - 16:29
    So we use a lot of this.
  • 16:29 - 16:34
    And so this is a pattern of many counters
    with a dictionary, simultaneous counters.
  • 16:34 - 16:35
    We're counting a bunch of, we're looking
  • 16:35 - 16:39
    at a series of things, and we're going to
    simultaneously keep track
  • 16:39 - 16:43
    of a large number of counters, rather than
    just one counter.
  • 16:43 - 16:47
    How many names did you see total? Whatever,
    12. But how many of each name
  • 16:47 - 16:50
    did you see is a bunch of counters, so
    it's a bunch of simultaneous counters.
  • 16:52 - 16:57
    So a dictionary is, is great for this,
    a dictionary is great for this.
  • 16:57 - 16:59
    We, when we see somebody for the first
  • 16:59 - 17:00
    time, we can add an entry to the
    dictionary,
  • 17:00 - 17:04
    which is kind of like going oh,
    csev one,
  • 17:04 - 17:08
    and then Chen Wen one. Now these don't
    exist yet.
  • 17:08 - 17:10
    Right? So we've got csev one and Chen
    Wen one, so
  • 17:10 - 17:13
    that creates an entry and sticks a one in
    it and the
  • 17:13 - 17:17
    mapping between the key csev and the value
    one, the key Chen Wen
  • 17:17 - 17:20
    and the value one and then we say, hey
    what's in there?
  • 17:20 - 17:23
    Oh, we've got a csev is one and
    Chen Wen is one.
  • 17:23 - 17:26
    And then we see Chen Wen a second time,
  • 17:26 - 17:27
    so we'd add another number right there.
  • 17:27 - 17:31
    So this old number is one, we add one to
    it and we get
  • 17:31 - 17:35
    two and then we stick that back in and
    then we do the calculations.
  • 17:35 - 17:39
    We do a dump and say oh there's two in
    Chen Wen and one in csev.
  • 17:40 - 17:41
    Okay?
  • 17:42 - 17:46
    So this is a great data structure for the
    simutaneous counters like what's
  • 17:46 - 17:50
    the most common word, who had the most
    commits, da, da, da, da, da.
  • 17:51 - 17:54
    Now, everything we do we have to figure
    out
  • 17:54 - 17:56
    like, when you're going to get in trouble
    with Python.
  • 17:56 - 18:00
    When Python's going to give you the old
    thumbs down and say oh, you went too far.
  • 18:00 - 18:06
    So one thing Python does not like is if
    you reference a key before it exists.
  • 18:06 - 18:10
    We'll, we'll talk in a second how to
    work around this. But if you simply
  • 18:10 - 18:12
    create a dictionary and say, oh, print out
  • 18:12 - 18:15
    what's under csev, it gives you a
    traceback.
  • 18:15 - 18:16
    It's like,
  • 18:16 - 18:18
    I'm going to inform you that that's not
    there.
  • 18:18 - 18:20
    And it says key error, csev.
  • 18:20 - 18:25
    Now, the thing that allows us to solve
    this is the in operator.
  • 18:25 - 18:28
    We've used the in operator to see if a
    substring was in a string.
  • 18:28 - 18:30
    Or if a number was in a list.
  • 18:30 - 18:37
    So, so this in operator says, in operator
    says, hey, ask a question.
  • 18:37 - 18:42
    Is the string csev a current key in the
    dictionary ccc?
  • 18:43 - 18:46
    Is the string csev a current key in the
    dictionary ccc?
  • 18:46 - 18:48
    And it says, False.
  • 18:49 - 18:52
    So now we have something that doesn't give
    a traceback
  • 18:52 - 18:55
    that can tell us whether or not the key is
    there.
  • 18:55 - 18:57
    So if you remember the algorithm, the
    first time you see it, you
  • 18:57 - 19:01
    set them to one, and every other time, you
    add one to them.
  • 19:03 - 19:04
    So this is how we do that in Python.
  • 19:05 - 19:08
    So here's how we implement that program
    that I just gave you
  • 19:08 - 19:12
    in Python. So, here's our names.
  • 19:12 - 19:15
    It's shorter so my slide works better.
  • 19:15 - 19:17
    Here's a variable, our iteration variable,
    it's going to, you know,
  • 19:17 - 19:21
    go through all five of these one at a time.
  • 19:21 - 19:25
    And within the body of the
    loop we have an if statement.
  • 19:25 - 19:27
    If the name is not currently in the
  • 19:27 - 19:31
    counts dictionary, counts is the name of
    my dictionary.
  • 19:31 - 19:34
    If the name is not currently in the
    counts dictionary,
  • 19:34 - 19:35
    I say counts sub name equals one.
  • 19:36 - 19:40
    else, that must mean it's already there
    which means
  • 19:40 - 19:43
    it's okay to retrieve it, counts sub name
    plus 1.
  • 19:43 - 19:47
    We're going to add a 1 to it and stick it
    back in, okay?
  • 19:47 - 19:49
    And so when this finishes it's going to
    add
  • 19:49 - 19:53
    entries and then add one to entries that
    already exist.
  • 19:53 - 19:57
    And not traceback at all. And when we
    print it out we're going to see the counts.
  • 19:57 - 19:59
    And literally this could have gone
  • 19:59 - 20:02
    a million times and it would just be fine
    and it would just keep expanding.
  • 20:02 - 20:03
    Okay?
  • 20:05 - 20:07
    So this pattern of checking to see if a key
  • 20:07 - 20:11
    is in a dictionary, setting it to some
    number, or
  • 20:12 - 20:15
    adding one to it is a really, really common
    pattern.
  • 20:16 - 20:20
    It's so common, as a matter of fact, that
    there is a
  • 20:20 - 20:25
    a special thing built into dictionaries
    that does this for us, okay?
  • 20:25 - 20:27
    And there is this method called get.
  • 20:28 - 20:30
    And so, counts is the name of the
    dictionary,
  • 20:30 - 20:34
    get is a built-in capability of
    dictionaries.
  • 20:34 - 20:36
    And it takes two parameters.
  • 20:36 - 20:43
    The first parameter is a key name, like a
    string, like csev or chen wen or marquard.
  • 20:43 - 20:51
    And then the second parameter is a value
    to give back if this doesn't exist.
  • 20:51 - 20:54
    It's a default value if the key does not
    exist.
  • 20:54 - 20:56
    And there's no traceback.
  • 20:56 - 21:01
    So this way you can encapsulate, in effect,
    an if-then-else.
  • 21:01 - 21:06
    If the name parameter is in the counts,
    print the thing out, otherwise print zero.
  • 21:06 - 21:11
    So this expression will either get the
    number
  • 21:11 - 21:17
    if it exists or it will give me back a
    zero if it doesn't exist.
  • 21:17 - 21:19
    So this is really valuable.
  • 21:19 - 21:21
    Right? This is really valuable.
  • 21:21 - 21:23
    That's a really bad smiley face.
  • 21:23 - 21:29
    So this is really valuable because it,
    once, once we understand the idiom,
  • 21:29 - 21:33
    it really takes four lines of code and
    turns it into one line of code.
  • 21:33 - 21:35
    Because we're going to be doing this
    if-then-else all the time.
  • 21:36 - 21:39
    Now, and so we can reconstruct that loop
  • 21:39 - 21:44
    a lot easier and a lot more cleanly using this
    idiom, right?
  • 21:44 - 21:46
    It's something that looks kind of complex
    but you'll
  • 21:46 - 21:49
    get used to it really fast, okay?
  • 21:49 - 21:52
    So we have, everything here is the same,
  • 21:52 - 21:54
    we create an empty dictionary, we have five
    names to
  • 21:54 - 21:56
    go through, we're going to write a
    for loop
  • 21:56 - 21:58
    and it's going to go through each of
    those.
  • 21:58 - 22:05
    And then we're going to say counts sub name
    equals counts dot get the value stored
  • 22:05 - 22:08
    at name, and if you don't find it, give me
    back a zero.
  • 22:08 - 22:12
    And then whatever comes back, either the
    old value or
  • 22:12 - 22:17
    the zero, add 1 to that and then take that
    sum and stick it in counts name.
  • 22:18 - 22:20
    Okay? So this is either
  • 22:22 - 22:23
    going to create,
  • 22:26 - 22:30
    or it's going to update.
  • 22:30 - 22:33
    If there is no entry, it's going to create
    it and set it to one.
  • 22:33 - 22:37
    If there is an entry it's going to add one to
    the current entry.
  • 22:38 - 22:39
    Okay? So this is,
  • 22:43 - 22:45
    this line is kind of an idiom.
  • 22:47 - 22:48
    Read about it in the book, figure it out,
  • 22:48 - 22:50
    get used to the notion of what this is doing.
  • 22:50 - 22:53
    Understand what that is doing, okay?
  • 22:54 - 22:57
    Because I'm going to start using it as if
    you understand it.
  • 22:58 - 23:05
    So, the next problem is a problem of
    finding the most common word.
  • 23:05 - 23:08
    So, finding the most common, the top
  • 23:08 - 23:12
    five, is often a, a trigger that says, use
  • 23:12 - 23:14
    dictionaries because if you're going to
    have to count things up,
  • 23:14 - 23:16
    you're going to, you know, you don't
  • 23:16 - 23:18
    know what the most common thing is at the
    beginning.
  • 23:18 - 23:22
    First you have to count everything up, and
    dictionaries are a great way to count.
  • 23:22 - 23:25
    So here's a little problem and I would
    like you to read
  • 23:25 - 23:29
    this text and find me the most common word
    in the text.
  • 23:29 - 23:33
    And tell me what the most common word is
    and how many times
  • 23:35 - 23:37
    it occurs. Ready?
  • 23:37 - 23:40
    I'm going to give you a thousandth of a
    second, just like I would give a computer.
  • 23:40 - 23:42
    I would expect it'd be able to do this in
    a thousandth of a second.
  • 23:42 - 23:43
    [SOUND] There you go.
  • 23:43 - 23:46
    [BLANK_AUDIO]
  • 23:46 - 23:48
    Okay, I gave you five seconds.
    Time's up.
  • 23:48 - 23:49
    Did you get it?
  • 23:50 - 23:53
    Or did you say to yourself, you know what,
    I hate
  • 23:53 - 23:56
    that, it's no good, I think I'll write a
    Python program instead.
  • 23:56 - 23:59
    And he'll probably show me a Python
    program if I wait long enough.
  • 23:59 - 24:03
    So here's a slightly easier problem from
    the first lecture.
  • 24:03 - 24:04
    Ready?
  • 24:04 - 24:05
    It's the same problem.
  • 24:05 - 24:08
    Find the most common word and how many
    times the word occurs.
  • 24:08 - 24:12
    [BLANK AUDIO]
  • 24:12 - 24:34
    [MUSIC]
  • 24:35 - 24:40
    Did you get it?
    I believe the answer is, and I could look
  • 24:40 - 24:46
    really dumb here, oops, the answer is the,
    and I think it's seven times.
  • 24:46 - 24:48
    So, that's the right answer. Okay?
  • 24:48 - 24:50
    Again, things humans are not so good at.
  • 24:51 - 24:55
    So, here's a piece of code that's starting
    to combine some
  • 24:55 - 24:58
    of the things we've been doing in the past
    few chapters all together.
  • 24:58 - 25:01
    We are going to read a line of text,
  • 25:01 - 25:06
    split it into words, count the occurrence,
    how many times
  • 25:06 - 25:10
    each word occurs, and then print out a map.
  • 25:10 - 25:15
    So, so here's what we're going to do,
    we're going to say okay, start
  • 25:15 - 25:19
    a dictionary, an empty dictionary, read
    the line of input.
  • 25:20 - 25:27
    Then split it, remember, the split takes a
    string and produces a list.
  • 25:27 - 25:32
    So words is a list, line is a string, and
    then we'll print that out.
  • 25:32 - 25:34
    Then we're going to write a for loop
    that's going to go
  • 25:34 - 25:38
    through each of the words, and
    then create, use this idiom
  • 25:38 - 25:42
    counts sub word equals counts.get word, 0 + 1.
  • 25:42 - 25:45
    So this is going to do exactly what we talked
    about in the previous
  • 25:45 - 25:51
    couple slides back, either create the
    entries or add to those entries, okay?
  • 25:51 - 25:52
    And then we're going to print
  • 25:52 - 25:53
    them out.
  • 25:53 - 25:56
    So here's what that program does when it
    prints out.
  • 25:57 - 25:59
    Now this is actually one long line I'm
  • 25:59 - 26:01
    just cutting it so you can see it.
  • 26:01 - 26:05
    Here's this line we enter, and the words
    the, there's seven of them.
  • 26:05 - 26:08
    Then it takes this line and splits it into a
  • 26:08 - 26:11
    list, and there is the beginning and end
    of the list.
  • 26:11 - 26:14
    The list maintains the order, so the
  • 26:14 - 26:18
    list simply breaks all these words into
    separate
  • 26:18 - 26:22
    words in a list of strings.
    From one string
  • 26:23 - 26:29
    to many strings. This is many strings.
    And so the, and the spaces are gone.
  • 26:29 - 26:31
    And so now here's this list.
  • 26:31 - 26:34
    And then what we're going to do is we're
    going to run through the list.
  • 26:35 - 26:39
    And we're going to keep running totals of
    each of the words in the list.
  • 26:39 - 26:40
    And then when we're done with the list,
  • 26:40 - 26:44
    we're going to print out the contents of
    that dictionary.
  • 26:44 - 26:45
    And we can inspect it and
  • 26:45 - 26:47
    go like, let's look for the biggest one,
    na, na, na, na, na.
  • 26:47 - 26:48
    It's kind of like
  • 26:48 - 26:51
    looking for the largest, like, oh,
    seven.
  • 26:51 - 26:54
    That's the largest and the largest word is
    the.
  • 26:54 - 26:55
    Okay?
  • 26:55 - 26:59
    So that's how the program runs, it
    reads a line,
  • 26:59 - 27:02
    splits it into a list of words, and then
  • 27:02 - 27:05
    accumulates a running total for each word,
    and then we
  • 27:05 - 27:09
    hand inspect to see what the most common
    word is.
  • 27:09 - 27:09
    Okay?
  • 27:11 - 27:13
    Oh no, no, I don't want that song again.
  • 27:13 - 27:14
    There we go.
  • 27:14 - 27:18
    And so and so here we have the, in it's
    kind of a smaller fashion.
  • 27:19 - 27:24
    We make a dictionary.
    This entering a line of text is here.
  • 27:24 - 27:25
    It's all one line.
  • 27:25 - 27:27
    We do the split and then we print the
    words out.
  • 27:29 - 27:32
    And so that split creates a list of
    strings from a single
  • 27:32 - 27:37
    string based on where the blanks are at,
    chop, chop, chop, chop.
  • 27:37 - 27:38
    And then here
  • 27:38 - 27:39
    at counting,
  • 27:41 - 27:46
    we're going to loop through each of the
    words one at a time and use this idiom,
  • 27:46 - 27:53
    counts sub word equals counts.get word, 0 + 1,
    which is going to create and/or update.
  • 27:53 - 27:55
    And then we print the counts out and that
    comes out there.
  • 27:56 - 27:57
    Okay?
  • 27:58 - 28:00
    So, again, this is the new thing that
    we've done.
  • 28:00 - 28:02
    Everything else we've kind of seen before.
  • 28:05 - 28:08
    Now we can also loop through dictionaries
    with for loops.
  • 28:13 - 28:15
    The for loop, we've been, put all kinds of
    things over here.
  • 28:15 - 28:19
    We've put strings over here, we've put
    lists of numbers over here.
  • 28:19 - 28:21
    We've put files over here.
  • 28:21 - 28:23
    And basically what it really says is you
  • 28:23 - 28:26
    know, if this is a collection of things,
  • 28:26 - 28:28
    run this little indent code once for
    each item in
  • 28:28 - 28:33
    the collection, and key then becomes our
    iteration variable.
  • 28:33 - 28:35
    And key is very mnemonic here.
  • 28:35 - 28:37
    It doesn't know that they are keys.
  • 28:37 - 28:39
    And so, keys.
  • 28:39 - 28:45
    The key here is that, there's a bit, the
    important
  • 28:45 - 28:50
    concept here is that dictionaries are
    key/value pairs and so this is
  • 28:50 - 28:53
    only one variable and so it actually
    decides that, they've decided that
  • 28:53 - 28:56
    it goes through the keys, which is
    actually quite useful.
  • 28:56 - 29:01
    So key is going to take on the successive
    values of the labels.
  • 29:01 - 29:02
    Not the successive values of
  • 29:02 - 29:04
    the values stored at the labels.
  • 29:04 - 29:10
    But it's really easy for us to retrieve
    the contents at that label counts sub key.
  • 29:10 - 29:15
    So we're going to use the key 'chuck',
    'fred', 'jan', to look up the 1, 42, 100.
  • 29:15 - 29:18
    And so it prints out the key,
  • 29:18 - 29:22
    and then the value at it, the key, and the
    value at it, and the key, and the value.
  • 29:22 - 29:25
    And so we're able to sort of go through
  • 29:25 - 29:27
    the dictionary and look at all the
    key/value pairs,
  • 29:27 - 29:30
    which is the common thing that you really
    want to do.
  • 29:31 - 29:32
    Okay?
  • 29:35 - 29:38
    Now there's some methods inside of
    dictionaries that allow
  • 29:38 - 29:43
    us to convert dictionaries into lists
    of things.
  • 29:43 - 29:47
    And so if you simply take a dictionary, so
    here's a little dictionary with
  • 29:47 - 29:52
    three items in it, and we can say list sub
    and then give a dictionary name
  • 29:52 - 29:54
    right there, and then that converts it
    into a
  • 29:54 - 29:57
    list. But it's just a list of the keys.
  • 29:58 - 30:01
    We can also say jjj dot keys, kind of do
    the same thing.
  • 30:01 - 30:05
    Say give me a list consisting of the keys.
  • 30:05 - 30:10
    And then jjj dot values gives you a list
    of the values, 1, 42, and 100.
  • 30:10 - 30:13
    Of course they're not in the same order.
  • 30:13 - 30:16
    Now interestingly, as long as you don't
    modify the dictionary,
  • 30:16 - 30:20
    the order of these two things corresponds
    as long as
  • 30:20 - 30:23
    in between here you're not changing it.
    So the first jan maps to 100,
  • 30:23 - 30:25
    chuck maps to 1,
  • 30:25 - 30:28
    and fred maps to 42.
  • 30:28 - 30:30
    So the order, you can't predict the order
    they're
  • 30:30 - 30:32
    going to come out but these two things
    will
  • 30:32 - 30:35
    come out in the same order, whatever that
    order
  • 30:35 - 30:38
    happens to be. Okay, and so there's one
    more thing.
  • 30:39 - 30:44
    So we've got the keys, we've got the
    values, and we've got a thing called items.
  • 30:44 - 30:50
    items also returns a list, it's a list.
    But it's a list of
  • 30:50 - 30:55
    what Python calls tuples.
    That's what the next chapter is about.
  • 30:55 - 30:57
    We'll talk more about tuples in the next
    chapter.
  • 30:58 - 31:01
    A tuple is a key/value pair.
  • 31:01 - 31:06
    So this list has three things in it.
    One, two, three.
  • 31:06 - 31:10
    The first one jan maps to 100, the
    second is chuck maps to 1, the
  • 31:10 - 31:16
    third one is fred maps to 42. So,
    just kind of bear with me for a second.
  • 31:16 - 31:18
    We'll hit this a little harder in the next
    chapter.
  • 31:19 - 31:21
    But the place that this, the idiom where
  • 31:21 - 31:24
    this works very beautifully is on a for
    loop.
  • 31:24 - 31:27
    Now, for those of you who have programmed
    in other languages, this will be
  • 31:27 - 31:30
    kind of weird because other languages have
  • 31:30 - 31:34
    iterations but they don't have two
    iteration variables.
  • 31:34 - 31:36
    Python has two iteration variables.
  • 31:36 - 31:37
    It can be used for many things but one of the
  • 31:37 - 31:41
    things that it's used for that's really
    quite nice is
  • 31:41 - 31:46
    we can have two iteration variables.
    This jj items returns pairs of
  • 31:46 - 31:51
    things and then aaa and bbb are iteration
    variables that sort of
  • 31:51 - 31:57
    move in synchronized, move, are
    synchronized as they move through.
  • 31:57 - 32:01
    So aaa takes on the value of the key.
  • 32:01 - 32:06
    bbb takes on the value of the, the
    value.
  • 32:06 - 32:09
    And then the loop runs once.
  • 32:09 - 32:13
    Then aaa is advanced to the next key.
  • 32:13 - 32:17
    And bbb is advanced to the next value
    simultaneously, synchronized.
  • 32:17 - 32:20
    Then they print that out, then it advances
    to the
  • 32:20 - 32:23
    next one, and the next one, and they print
    that out.
  • 32:23 - 32:27
    So they are moving in a synchronized way.
  • 32:27 - 32:31
    Now again, the order jan, chuck, fred is not
    the same.
  • 32:31 - 32:33
    But the correspondence between jan 100,
  • 32:33 - 32:37
    chuck 1, and fred,
    that's going to, that's going to work.
  • 32:37 - 32:41
    And so basically, as these things go, they
    work
  • 32:41 - 32:44
    their way through whatever order they're
    stored in the dictionary.
  • 32:44 - 32:45
    So this is quite nice.
  • 32:45 - 32:49
    Two iteration variables going through
    key/value.
  • 32:49 - 32:54
    Now if I was making these names mnemonic,
    and they made more sense,
  • 32:54 - 32:57
    I would call this the key variable and
    that would be the value variable.
  • 32:58 - 33:01
    But for now I'm just using kind of silly
    names
  • 33:01 - 33:03
    to show you that key and value are not
    special.
  • 33:03 - 33:06
    They're not Python reserved words in any
    way.
  • 33:06 - 33:09
    They're just a good way to name these
    things, key/value pairs.
  • 33:09 - 33:10
    Okay?
  • 33:12 - 33:13
    Okay.
  • 33:13 - 33:17
    Now we're going to circle all the way
    back to the beginning.
  • 33:17 - 33:18
    All the way back to the first lecture.
  • 33:18 - 33:24
    And I gave you this program, and I said
    don't worry about it.
  • 33:24 - 33:28
    We'll learn about it later.
    Well, now later.
  • 33:28 - 33:32
    At this point you should be able to
    understand every line of this program.
  • 33:33 - 33:38
    This is the program that's going to count
    the most common word in a file.
  • 33:38 - 33:39
    Okay?
  • 33:39 - 33:41
    So let's walk through what it does and
    hopefully
  • 33:41 - 33:45
    by now this will make a lot of sense.
  • 33:46 - 33:48
    Okay? So we're going to start out, we're
    going to ask
  • 33:48 - 33:51
    for a file name, we're going to open that
    file for read.
  • 33:52 - 33:55
    Then, because we know it's not a very large
  • 33:55 - 33:57
    file, we're going to read it all in one go.
  • 33:57 - 34:00
    So handle dot read says read the whole
    file, newlines and all,
  • 34:00 - 34:04
    blanks, newlines, whatever,
    and put it in
  • 34:04 - 34:08
    the variable called text, it's just
    mnemonic. Remember I'm, in this one
  • 34:08 - 34:13
    I'm using the mnemonic variable names.
    Then go through that whole
  • 34:13 - 34:17
    string, which was the whole file, go
    through and split it all.
  • 34:17 - 34:20
    Newlines don't hurt it.
    Newlines are treated like blanks.
  • 34:20 - 34:22
    And it understands all that.
  • 34:22 - 34:23
    It throws the newlines away and the
    blanks away
  • 34:23 - 34:27
    and splits it into a beautiful list of
    just words with no blanks.
  • 34:29 - 34:33
    And the list of the words in that file
    ends up in the variable words.
  • 34:33 - 34:36
    words is a list, text is a string, words
    is a list.
  • 34:37 - 34:42
    Then what I do is the pattern of
    accumulating counters in a dictionary.
  • 34:42 - 34:44
    I make an empty dictionary.
  • 34:44 - 34:48
    I have the word variable that goes through
    all the words
  • 34:48 - 34:53
    and then I just say, counts sub word equals
    counts dot get(word,0) + 1,
  • 34:54 - 34:57
    and that, like we just got done saying,
    it both creates
  • 34:57 - 35:02
    and/or increments the value in the
    dictionary as needed.
  • 35:02 - 35:04
    So now at the end of the, at the, at this
  • 35:04 - 35:12
    point in the program, we have a full
    dictionary with the word:count.
  • 35:12 - 35:12
    Okay?
  • 35:12 - 35:15
    And there's many of them.
    You know, all the words, all the counts.
  • 35:15 - 35:17
    They're not in any particular order. So now what
  • 35:17 - 35:22
    we're going to do is we're going to write
    a largest loop, find the largest.
  • 35:22 - 35:24
    Which is another thing that we've done.
  • 35:24 - 35:27
    So not only do I need to now know what
    largest count I've seen so far,
  • 35:27 - 35:30
    I need to know what word that is.
  • 35:30 - 35:33
    So I'm going to set the largest count
    we've seen so far to None, set
  • 35:33 - 35:37
    the largest word we've seen so far
    to None, and then I'm going to use this
  • 35:37 - 35:39
    two-iteration variable pattern to say
  • 35:39 - 35:44
    go through the key/value pairs word and
    count in counts.items.
  • 35:44 - 35:45
    So it's just going to
  • 35:45 - 35:47
    go through [SOUND] all of them.
  • 35:47 - 35:53
    And then I'm going to ask if the largest
    number I've seen so far is None or
  • 35:53 - 35:56
    the current count that I'm looking at is
    greater then the largest I've seen so far,
  • 35:59 - 36:03
    keep them. Take the current word, stick it
    in biggest word so far,
  • 36:03 - 36:07
    take the current count, stick it in
    the biggest count so far.
  • 36:07 - 36:10
    So this is going run through all of the
  • 36:10 - 36:14
    word.count pairs, word.count key/value pairs.
  • 36:14 - 36:17
    And then when it comes out, it's going to
    print out
  • 36:17 - 36:19
    the word that's the most common and how
    many times.
  • 36:21 - 36:24
    So if we feed in that clown text, it will
    run all this stuff, and print out
  • 36:24 - 36:29
    oh, the is the most common word, and it
    appeared seven times.
  • 36:29 - 36:34
    Or if I print the stuff that was two
    slides back, words.txt, from the actual
  • 36:34 - 36:38
    textbook, then it says the word to is the
    most common and it happened 16 times.
  • 36:38 - 36:43
    So I could easily, you know, throw 10
    million, 10 million
  • 36:43 - 36:46
    words through this thing, and it would
    just be totally happy.
  • 36:46 - 36:49
    Right? And so, this is not that complex
  • 36:49 - 36:53
    of a problem, but it's using a whole bunch
    of idioms that we've been using.
  • 36:53 - 36:57
    The splitting of words, the accumulation
    of multiple counters in a dictionary.
  • 36:57 - 37:02
    And so, it sort of is the beginning of
    doing some kind of data
  • 37:02 - 37:06
    analysis that's hard for humans to do, and
    error-prone for humans to do.
  • 37:06 - 37:08
    And so this is, we're reviewing collections.
  • 37:08 - 37:11
    We've introduced dictionaries.
  • 37:11 - 37:13
    We've done the most common word pattern,
    talked about that.
  • 37:13 - 37:14
    The lack of order, and
  • 37:14 - 37:16
    I did that a bunch of times.
  • 37:16 - 37:20
    And we've looked ahead at tuples,
    which is the next,
  • 37:20 - 37:22
    the third kind of collection that we're
    going to talk about.
  • 37:22 - 37:26
    And they're actually in some ways a little
    simpler than dictionaries.
  • 37:26 - 37:27
    And simpler than lists.
  • 37:27 - 37:33
    So, see you in the next lecture, Chapter
    10, tuples.
Title:
Python for Informatics - Chapter 9 - Dictionaries
Description:

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

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

English subtitles

Revisions