Python Learning 04--list operations

Source: Internet
Author: User

Stupid way to learn the 38th verse of Python

How to create a list in the 32nd section, in the following form:

This section is mainly about the operation of the list, first of all the work of mystuff.append (' Hello '), I understand that, first Python find mystuff this variable, and then append () this function operation. One thing to note is that the parentheses () have an extra parameter that is mystuff itself.

This article exercises:

1 #Create a mapping of state to abbreviation2states = {3     'Oregon':'OR',4     'Florida':'FL',5     'California':'CA',6     'Michigan':'MI'7 }8 9 #Create a basic set of States and some cities in themTenCities = { One     'CA':'San Francisco', A     'MI':'Detroit', -     'FL':'Jacksonville' - } the  - #add some more cities -cities['NY'] ='New York' -cities['OR'] ='Portland' +  - #print out some cities + Print '-'* 10 A Print "NY State have:", cities['NY'] at Print "OR State has:", cities['OR'] -  - #Print Some states - Print '-'*10 - Print "Michigan ' s abbreviation is:", states['Michigan'] - Print "Florida ' s abbreviation is:", states['Florida'] in  - #Do it by using the and then cities Dict to Print '-'*10 + Print "Michigan has:", cities[states['Michigan']] - Print "Florida has:", cities[states['Florida']] the  * #print every state abbreviation $ Print '-'*10Panax Notoginseng  forState, AbbrevinchStates.items (): -     Print "%s is abbreviated%s"%(State, abbrev) the  + #Print every city on state (why not sequence) A Print '-'*10 the  forAbbrev, CityinchCities.items (): +     Print "%s has the city%s"%(abbrev, city) -  $ #Now does both at the same time $ Print '-'*10 -  forState, AbbrevinchStates.items (): -     Print "%s State is abbreviated%s and have city%s"% ( the State , Abbrev, Cities[abbrev]) - Wuyi Print '-'*10 the #safely get a abbreviation by state the might not being there -State = States.get ('Texas', None) Wu  - if  notState : About     Print "Sorry, no Texas." $  - #get a city with a default value -City = Cities.get ('TX','Does not Exist') - Print "The city for the state ' TX ' is:%s"% City

Problems that exist:

1.40 rows of the output city name, the runtime output is not sequential output, what is the rule of this output follow?

2. States.items (), according to the book said () there is an additional parameter states, so there is no parameter in this (), because the parameter is the previous list, if you want to add a parameter in the (), how to add?

Reply:

1. Python Built-in dictionary: dict support, Dict full name dictionary, also known as map in other languages, using key-value (Key-value) storage, with extremely fast lookup speed.

The order in which the dict is stored is not related to the order in which the key is placed, and the Dict lookup is based on the hash table, so the output is not sequential.

2. This is not the same function as in the book, the function in the book is append (), the item () is the equivalent of traversing the list, so no arguments are added later.

Python Learning 04--list operations

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.