The python dictionary is based on the rough understanding of the python ancestor and dictionary and set.

Source: Internet
Author: User

The python dictionary is based on the rough understanding of the python ancestor and dictionary and set.

I. yuanzu (tuple)

1. The ancestor cannot be modified. It is usually written as a series of items in parentheses, which have an orderly position and fixed length.

2. In fact, the original database supports the general sequence operations of strings and lists. When "+", "*", and the multipart operations are applied to the original database, a new ancestor is returned.

print((1,2)+(3,4))>>>>(1,2,3,4)print((1,2)*4)>>>>(1,2,1,2,1,2,1,2)T=(1,2,3,4)print(T【0】,T【1:3】)>>>>(1,(2,3))

3. The ancestor does not provide a string, list, or dictionary method. If you add a stock to the parent database, you must first convert it into a list to obtain the call using the sorting method.

T=(“z”,"b"."c")tem= list(T)tem.sort()print(tem)>>>> ["b","c","z"]T=tuple(tem)print(T) >>>>("b","c","z")

However, the internal list of the ancestor can be modified as usual.

T=(1,[2,3],4)T[1].[0] ="ABC"print(T)>>>>>(1,["ABC",3],4)

2. Dictionary (dict)

1. The dictionary is unordered, that is, the sorting of the result data in each query is not necessarily because the key-value type data does not need to be indexed by subscript.

2. dictionary operations:

D1 ={}# indicates an empty dictionary D2 = {"spam": 1, "app": 2} # Two-Project dictionary D3 = {"food": {"spam ": 1, "egg ": 2 }}# nested D2 ["app"] # search for D3 ["food"] ["spam"] "egg" in D3 # identify whether egg exists in D3, if a key exists, TrueD2.keys () # query key value D2.values () # query value D2 [key] = 44 # indicates addition or modification. If the key does not exist in the dictionary, if yes, modify del D2 [key] # Delete
D2 = {"egg", 1, "app", 2} print (D2 ["app"]) >>>>>>> 2 print (D2) >>>>>>> {"egg", 1, "app", 2} len (D2) >>>>>>>> 2 # how to merge the length of the returned keys list: D2 = {"egg", 1, "app", 2} D3 = {"egg", 1, "app", 2} D2.update (D3) print (D2) >>>>>>> {"egg", 1, "app", 2, "egg", 1, "app", 2} pop deletion method: You can delete a dictionary key and return its value D2 = {"egg", 1, "app ", 2} D2.pop ("egg") >>>>>>>>> 1 print (D2) >>>>>>>{ "app ", 2} another creation method: the condition is that the values of all keys are the same dict. fromkeys (["a", "B"], 0) >>>>>>>>>>> {"a": 0, "B": 0}

3. set)

A set is unordered and does not overlap repeated data. Mainly used

(1) de-duplication; convert a list into a set to automatically remove duplicates

(2) Test the link. Test the intersection and union of the two groups of data.

Related Operations

S1 = set ([, 7]) s2 = set ([,]) print (s1.intersection (s2 )) # indicates taking the intersection >>>>>>>>>{ 2, 4, 5, 6, 3} print (s1.union (s2 )) # indicates taking the Union set >>>>>>>>> {1, 2, 3, 4, 5, 6, 7, 9} print (s1.difference (s2 )) # indicates the difference set >>>>>>>>>{ 7, 9} print (s1.isdisjiont (s2 )) # indicates whether s and s2 have an intersection >>>>>>>> Trues1.add (10) # Add 1 s1.update ([, 10]) # add multiple items s1.remove (1) # delete an item. The value is 1 (specify which item to delete, and if no error is specified) s1.pop () # delete a random number

The above is a rough understanding of the python ancestor and the dictionary and set. I hope you can give me a reference and support the house of helpers.

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.