Python list and for loop knowledge points to note

Source: Internet
Author: User
Tags python list


Lists can be nested in different data types, as follows: nested dictionaries in lists

A = [{' Name ': ' Zhouziqi ', ' Contact ': 17806762943},{' name ': ' Zhouyu ', ' contact ': 13246910612}]for i in A:print (i)

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/BD/wKiom1m9MTfhOhXeAAARW93kFck588.png "title=" _ 20170916210710.png "alt=" Wkiom1m9mtfhohxeaaarw93kfck588.png "/>

We can use a for loop to show each dictionary

If you want to find a name for Zhouziqi information, the idea is that, let a list of each dictionary assigned to I, so I is a dictionary, if you want to find the dictionary "name" can be used i[' name ')

A = [{' Name ': ' Zhouziqi ', ' Contact ': 17806762943},{' name ': ' Zhouyu ', ' contact ': 13246910612}]for i in A:print (i[' name '])

This will always be used in the future.

Below we say a For loop to pay attention to the small point, look directly at the example:

A = [{' Name ': ' Zhouziqi ', ' Contact ': 17806762943},{' name ': ' Zhouyu ', ' contact ': 13246910612}]name = input ("Please input  Name: ") for I in a: #把a列表中的字典一个个的赋予给变量i if name = = i[' name ']: #判断用户输入name的值是否等于在i字典中key为name的值 print (" The name is Exist ") #如果存在就输出正确信息 Else:print (" None ") #这是不正确的信息

There is nothing wrong with the above code, you can do it:

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/06/BD/wKiom1m9NJPRKBCqAAAMSbizaQc023.png "title=" _ 20170916210710.png "alt=" Wkiom1m9njprkbcqaaamsbizaqc023.png "/>

Why is it that we entered the name of the Zhouziqi, but said that the name exists, and that the name does not exist?

Because there are two dictionaries in our a list, when we execute the second line of the above code, that is, let the user enter a value, we enter Zhouziqi, this time name is equal to Zhouziqi, enter the third line of code, I the first value is the dictionary {"name": ' Zhouziqi ', ' Contact ': 17806762943}, then the fourth line of code to determine whether the value of name equals {"name": ' Zhouziqi ', ' Contact ': 17806762943} The value of key is name in the dictionary, The result Zhouziqi is equal to Zhouziqi, this time the equation is set up, the fifth line of code, print the correct information, this appears the name is exist, then why there is a none error message printed out, because a is two dictionaries, just we {"name": ' Zhouziqi ', ' Contact ': 17806762943}, now use the second dictionary {"name": ' Zhouyu ', ' Contact ': 13246910612}, the error message will be printed if not found. This is not the same as we think, then we can let it judge the name of the break out of their own. This is still a problem. Oh, look at the following code

A = [{' Name ': ' Zhouziqi ', ' Contact ': 17806762943},{' name ': ' Zhouyu ', ' contact ': 13246910612}]name = input ("Please input Name: ") for i in a:if name = = i[' name ']: print (" The name is exist ") Break Else:print (" None ")

Let's do this, enter Zhouyu

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/06/BE/wKiom1m9N3aCXZE2AAAPWz0I7_4028.png "title=" _ 20170916210710.png "alt=" Wkiom1m9n3acxze2aaapwz0i7_4028.png "/>

Why there is none, because as above, the first value of I is {"name": ' Zhouziqi ', ' Contact ': 17806762943} to determine that the name (i.e. Zhouyu) is not equal to i[' name '] (that is, Zhouziqi So just print out none, and then the second dictionary is {"name": ' Zhouyu ', ' Contact ': 13246910612}, which matches the successful output of the name is exist. So what should we do?

Because no matter what, there will be none, then we can not do this else, the answer is OK, you can not this else, and then on the outside to define a value of False_number default value is 0, and then within the For loop, If we find the name that the user entered to change the value of False_number to 1, and then break out of the loop, outside we go to determine whether False_number is 1, if this name exists in the dictionary in the array, otherwise there is no

A = [{' Name ': ' Zhouziqi ', ' Contact ': 17806762943},{' name ': ' Zhouyu ', ' contact ': 13246910612}]name = input ("Please input Name: ") False_number = 0for i in a:if name = = i[' name ']: False_number = 1 if False_number = = 1:breaki F False_number = = 1:print ("The name is Exist") Else:print ("None")


This article is from "Love Zhou Yu" blog, please be sure to keep this source http://izhouyu.blog.51cto.com/10318932/1965950

The list of Python and the knowledge points to note for the For loop

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.