Python 7 list for dictionary, nested

Source: Internet
Author: User

List:

Basic format: variable name = [element 1, Element 2, Element 3]

Create: A = [' Visitor ', ' admin ', 19] or A = List ([' Armin ', ' admin ', 19]), which prefers to convert to a list of functions.

Directly define the first kind.

In judgment: If "Guest" in A: used to determine if a string visitor is included in a. The smallest unit that is judged here is the three elements in the list.

You cannot judge a more detailed character in an element.

If 9 in a[2]: used to determine if the number 9 is contained in the third element of the A list. This is used to determine only one of the elements in the list

Contains 9 of this character.

Index: a[0]

Slices: Same as String. For example A[1][1:3] If you write only to the first parenthesis, the smallest unit of the slice is the element in the list.

If a second parenthesis is written, the smallest unit is the character in the element.

Length: Len (A) The length of the list is the number of elements in the list. Rather than the number of characters.

To loop through a list element:

A = List (['Armin','admin',+]) i = 0 whilei <print(a[i]) i + = 1 

Append element: A.append ("ddd") adds an element to the tail of the list.

Insert element: A.insert (0, "the element to be inserted") means inserting the element to be inserted before the position No. 0 of the list A

Delete: A.remove (Element) but only the elements on the first match from left to right are deleted.

Del a[0] Deletes the elements in list A by location. Can be deleted using the Del a[0:] Slice.

Modified: a[0] = "ADSFASDF" modified to "ASDFASDF" for the No. 0-bit element of list A

For loop:

Basic structure:

A = ["admin","Guest", "+" inPrint (custom) #即可循环打印出列表A当中的所有元素. # break # Continue        
Dictionary: D = {key: value, key: Value}

Create: V = {"name1": "admin"

"Name2": "Guest"

}

Or:

V = Dict ({"name1": "admin"

"Name2": "Guest"

})

Value: v[' name1 ') to remove the value corresponding to the ' name1 ' key in the dictionary V.

Add modified element: V[name3] = "hahaha" If the dictionary contains name3 then that is the modification, if not it will add Name3 in the dictionary.

Delete: Del d["Name3"]

For loop Fetch dictionary value:

    fetch key: for custom in D: or for Custom in D.keys (): Print (Custom)

D = {    "name1":"admin""name2":"[' admin ', ' Mark ']" }inprint (AAA)              

value:for Custom in D.values () to show print (custom)

D ={"Name1 ": " admin"  "name2  ": " [' admin ', ' Mark ']< Span style= "COLOR: #800000" > "}for aaa in D.values (): #aaa是自定义的,. Values is a keyword. print (AAA) output: [ "admin ", mark "]admin   

    Key-value pairs: for key,val in D.items (): The embodiment of print (Key,val) key,val is casually defined. Commas are required.

B{"Name1 ": " admin"  "name2  ": " [' admin ', ' Mark ']< Span style= "COLOR: #800000" > "}for key,val in D.items (): @.items is the keyword Key.val is custom, but the comma in it is required print (key,val) output: name1 adminname2 [" admin ", mark "        

For custom in D.items (): Similar to above, but output results

B{"Name1":"Admin","name2" [' admin ', ' Mark ']" }for aaa in D.items (): #aaa是自定义的,. Items is a keyword. print (AAA) output: ( "name2 ", [' admin ', ' Mark ']name1 ",  ' admin      
Nesting of lists and dictionaries:

List nested lists:

List = [‘Armin ",  ' admin", 1234,[ ' a  ", " b, ', 11armin ",  "admin", 1234, [ ' a ",  ' b, ", [one]]     

Dictionary nesting:

A_dict ={‘Key1‘:"Value1",‘Key2‘:{‘K1‘:‘V1‘,‘K2‘:[‘vv1 ",  ' vv2 ' "},  "key3 ": 886,  "key4": [ ' armin", 

Modifications to dictionaries and lists in nested:

A_dict ={‘Key1‘:"Value1",‘Key2‘:{‘K1‘:‘V1‘,‘K2‘:[‘Vv1‘,‘Vv2‘]},‘Key3': 886,‘Key4‘:[‘Armin‘,‘Admin ", [1,2,3],{" k41 ": " v41  ' }]} A_dict[ "key4"][3][ ' n "]=  "test" #向 Keys4 in the fourth element Add a key n value to test. a_dict[ "key4print (a_dict)            

Nested applications:

A_dict =[    {"Name" :"Armin","Pwd" :"123"}, {"Name" :"Admin","Pwd" :"123"}, {"Name" :"Acmin","Pwd" :"123"}]username = input ("Please enter user name:") Password = input ("Please enter your password:")For AAAin a_dict: if username = = aaa["name"] and password = = aaa["pwd"]: prin T (" not Easy ") else: print (" please check user name password ")   break  

Python 7 list for dictionary, nested

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.