Python Ganso with dictionaries and examples of collections

Source: Internet
Author: User
The following small series for everyone to bring a based on the Python Ganso and dictionaries and sets of superficial understanding. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

One, Ganso (tuple)

1. The meta-ancestor is not modifiable, usually written in parentheses in a series of items, the position of the orderly, fixed length

2. In fact, the meta-ancestor supports the general sequence operation of strings and lists, "+", "*", and the Yuanju when the Shard operation is applied will return a new Ganso


Print ((+) + (3,4)) >>>> (1,2,3,4) print ((0) >>>> (1,2,1,2,1,2,1,2) t= (1,2,3,4) print (T " ", T" 1:3 ") >>>> (1, (2,3))

3. The meta-ancestor does not provide the method of string, list and dictionary, take a stake in your relative progenitor to sort, usually first you have to convert it to a list in order to get a 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 " ")

But the list inside the meta-ancestor can be modified as usual.


t= (1,[2,3],4) t[1]. [0] = "abc" Print (T) >>>>> (1,["abc", 3],4)

Two. Dictionary (dict)

1. The dictionary is unordered, that is, the result data of each query you order is not necessarily, because it is key-value type of data, do not need to pass the subscript index

2. Operation of the Dictionary:


d1={} #表示空字典D2 ={"spam": 1, "App": 2} #两项目字典D3 ={"food": {"spam": 1, "Egg": 2}} #嵌套D2 ["Apps"] #通过键进行查找D3 ["Food"] "spam" "Egg In D3 #判断egg是否存在在D3中, the Presence Returns Trued2.keys () #查询key值D2. VALUES () #查询value值D2 [key]=44# means new or modified, when the key is not present in the dictionary is added, the presence is modified Del D2[key] #删除


D2 = {"Egg", 1, "app", 2}print (d2["app"]) >>>>>>>2print (D2) >>>>>>>{"Egg", 1 , "App", 2}len (D2) >>>>>>>2# returns the length of the list of keys when merging methods: D2 = {"Egg", 1, "app", 2}d3 = {"Egg", 1, "app", 2}d2.update (D3) print (D2) >>>>>>>{"Egg", 1, "app", 2, "egg", 1, "app", 2}pop Delete method: Ability to delete a dictionary key and return its value D2 = {"Egg", 1, "app", 2}d2.pop ("egg") >>>>>>>>1print (D2) >> >>>>>>{"App", 2} Another method of creation: The condition is that all key values are the same dict.fromkeys (["A", "B"],0) >>>>>> >>>{"A": 0, "B": 0}

Three. Set (set)

A collection is an unordered, non-repeating data coincident. The main function is to

(1) go to the weight; turn a list into a collection and automatically remove duplicates

(2) Relationship test; Test the intersection of two sets of data, and set the relationship

Related actions


s1= set ([1,2,3,4,5,6,7]) s2 = set ([2,5,4,6,3,9]) print (s1.intersection (S2)) # Represents a fetch intersection >>>>>>>>{2,4,5,6,3}print (s1.union (S2)) #表示取并集 >>>>>>>>{ 1,2,3,4,5,6,7,9}print (s1.difference (S2)) #表示差集 >>>>>>>>{7,9}print (S1.isdisjiont (S2)) # Indicates whether S and S2 have intersection >>>>>>>>trues1.add (Ten) #添加1个项s1. Update ([8,9,10]) #添加多个项s1. Remove (1) #删除一项, A value of 1 (specifies which to delete, does not specify an error) S1.pop () #随机删除一个数 

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.