Placeholder Image

Subtitles section Play video

  • A standard data structure in computer science is theassociative arraywhich is also

  • called a “map.” In Python, this structure is called a “dictionary.” Dictionaries

  • are used when you have key/value pairs of data - an input which is mapped to an output.

  • If we had a dictionary that stored the quality of YouTube videos, then the value for this

  • lesson would be 17.8...

  • Suppose we are creating a new social network calledFriendfacewhere any life form

  • or intelligent agent is allowed to join.

  • Let us make our first post to Friendface... We will use this post to show you how to create

  • a dictionary.

  • To begin, we will collect several pieces of data for each post:

  • The user id... message... language... datetimeand the location from whence the post was

  • made.

  • Using dictionaries, we can store all of this data in a single object.

  • To create a dictionary, use the left brace, then list the key name, a colon, and the value.

  • If you have more than one key/value pair, you separate them using a comma.

  • Once you have finished entering your data, use a right brace to finish the dictionary.

  • We have created a dictionary calledpostwith 5 pieces of data. If you think of a dictionary

  • as a map, there are 5 inputs, and 5 outputs. In Python, inputs are called keys, and outputs

  • are called values.

  • Notice how the values have a variety of data types in this dictionary: an integer, three

  • strings... and a tuple of floats. Python is flexible: you can use all kinds of data types

  • for both keys and values. You are free to mix and match data to suit your needs.

  • If you use thetypefunction, you will seepostis an instance of thedict

  • class. This suggests we can use thedictconstructor to create a dictionary, and this

  • suggestion is correct. Reminder - we are using Python version 3 in this video.

  • Let’s create another Friendface post using thedictconstructor. This time, we

  • will only provide a few pieces of dataYou can see this made a dictionary.

  • We add additional pieces of data by using brackets.

  • The key name goes inside the brackets, and you use the equals sign to assign the value.

  • Notice that in the constructor, you did not have to put quotes around the key name, but

  • when you add new data using brackets, you DO use quotes.

  • If you print the dictionary, you will see all the data is there inside braces.

  • Just as you use brackets to store new data in a dictionary, you use brackets to access

  • data as well. Example: to see the message from the first post, use the key namemessage

  • inside brackets.

  • But do not be careless. Let’s see what happens if you try to access data that is not in the

  • dictionary. When we createdpost2’, we did not assign a location. If we try to access

  • this value, we get a Key Error.

  • How do we avoid such catastrophes? Simple. One way is to use theinoperator to

  • first check if a key is in the dictionary.

  • Another way is to try and retrieve the value, but handle the possibility of a KeyError.

  • To do this, type thetrycommand followed by a colon.

  • Next, attempt to print the location. If the dictionary does not have a ‘location

  • key, it will raise a KeyError. We handle this case by creating anexcept

  • block. Typeexceptand the possible errors you would like to handle. Here, we

  • want to handle the KeyError. If this occurs, the following code block will execute.

  • It works...

  • There is another way to access data in a dictionary and handle the possibility it does not have

  • a certain key. First, display the directory for thepost2’ dictionary. Here we see

  • a list of methods available to us. We will explore thegetmethod. To see what

  • this does, use the help function.

  • Thegetmethod lets you try and get the value for a specific key. If the dictionary

  • does not contain data for that key, you can specify a default value. We will use this

  • method to get thelocationfrom post2. If this post does not have a location, let’s

  • return None. If you print the value, you see the method

  • did returnNone.’

  • Turn your attention back to the original post.

  • A common task is to iterate over all the key/value pairs in a dictionary.

  • A straightforward way to do this is to loop over all the keys, then get the value for

  • each key. Thekeysmethod gives us an object we can loop over that contains all

  • the keys for the dictionary.

  • If you are using Python version 2, the output may look different.

  • This is because we are using Python 3, and in version 3, the print function became more

  • powerful.

  • The order of the data may be different for you. Do not panic. Dictionaries are not ordered

  • data. As long as you see all the data, everything is under control.

  • Another way to iterate over all the key/value pairs in a dictionary is to use theitems

  • method. This will give you both the key and the value in each step of the iteration.

  • There are a variety of methods for removing data from a dictionary.

  • Thepopandpopitemmethods allow you to remove a single item from a dictionary,

  • while theclearmethod will remove all data. After you complete this video, be sure

  • to experiment with these methods.

  • Look at me… I am a dictionary

A standard data structure in computer science is theassociative arraywhich is also

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it

B1

Python字典 || Python教程 || 學習Python編程 (Python Dictionaries || Python Tutorial || Learn Python Programming)

  • 15 0
    林宜悉 posted on 2021/01/14
Video vocabulary