Stupid way to learn Python (40)

Source: Internet
Author: User

Exercise 40: A dictionary, a lovely dictionary

Next I'm going to teach you a different kind of container-type data structure, because once you've learned this kind of container, you're going to have super cool power. This is the most useful container: the dictionary (dictionary).

Python calls this data type "Dict", and in some languages its name is "hash". I'll use both of these names, but it doesn't matter, it's the difference between them and the list. You see, for a list you can do something like this:

1>>> things = ['a','b','C','D']2>>> Print things[1]3 b4>>> things[1] ='Z'5>>> Print things[1]6 Z7>>>Print Things8['a','Z','C','D']9>>>

You can use numbers as the index of the list, that is, you can find the elements in the list by numbers. What dict does is that you can find the elements by anything, not just numbers. Yes, dictionaries can associate an object with something else, regardless of their type, let's take a look at:

1>>> stuff = {'name':'Zed',' Age': $,'Height':6* A+2}2>>> Print stuff['name']3 Zed4>>> Print stuff[' Age']5  $6>>> Print stuff['Height']7  About8>>> stuff[' City'] ="San Francisco"9>>> Print stuff[' City']Ten San Francisco One>>>

You'll see that in addition to passing numbers, we can also use strings to get stuff from a dictionary, and we can add elements to and from dictionaries. Of course it supports not only strings, we can also do things like this:

1>>> stuff[1] ="Wow"2>>> stuff[2] ="Neato"3>>> Print stuff[1]4 Wow5>>> Print stuff[2]6 Neato7>>>Print Stuff8{' City':'San Francisco',2:'Neato',9     'name':'Zed',1:'Wow',' Age': $,Ten     'Height': About} One>>>

I have used two numbers here. I can actually use anything, but it's not accurate, but you have to understand that first.

Of course, a dictionary that can only put things in is not very interesting, so we also have to delete the object method, that is, using the del keyword:

1>>> del stuff[' City']2>>> del stuff[1]3>>> del stuff[2]4>>>Stuff5{'name':'Zed',' Age': $,'Height': About}6>>>

Next we have to do an exercise, you must be very careful, I ask you to write down this exercise, and then try to understand what it does. This exercise is very interesting, you may have the feeling of being enlightened when you finish it.

1Cities = {'CA':'San Francisco','MI':'Detroit',2                      'FL':'Jacksonville'}3 4cities['NY'] ='New York'5cities['OR'] ='Portland'6 7 def find_city (Themap, State):8     ifState in Themap:9 return Themap[state]Ten     Else: OneReturn"Not found." A  -# OK Pay attention! -cities['_find'] =find_city the  -  whileTrue: -Print"State ? (ENTER to quit)", -State = Raw_input (">") +  -     if  notState:break +  A# This was the most important ever! study! atCity_found = cities['_find'] (cities, state) -Print City_found
View Code

Warning

Notice I used a themap instead of a map ? This is because Python already has a function called map, so if you use map as the variable name, you may encounter problems later.

The results you should see

Bonus points Exercise
    1. Find the relevant content of dictionary (also known as dicts, Dict) in the Python documentation and learn to do more with Dict.
    2. Find out what some dict can't do. For example, the more important one is that the content of dict is unordered, and you can check to see if this is true.
    3. Try putting For-loop on top of Dict, and then try using the Dict items () function in For-loop to see what the results will be.

Stupid way to learn Python (40)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.