What is a collection in python
Editor to share with you what is a collection in python, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
# set does not support indexing and slicing, so it is a non-repeatable container # similar to a dictionary, but only key does not have value# to create a collection dic1= {} set1= {1 minus 3} print (type (set1)) print (type (dic1))
Dic1= {} set1= {1 type (set1)) print (type (dic1)) set1.add ('python') # add data print (set1) # Clean operation set1.clear () print (set1)
The difference set print (set1.difference (set2)) of the two sets of set1= {1 set1.difference 3} set2= {2 Jing 3 pm 4} # difference
Set1= {1 set1.intersection 2 set2 3} set2= {2 Jing 3 4} # Union operation print (set1.intersection (set2)) # print the part where the two parts intersect
Set1= {1 set1.union 2 set2 3} set2= {2 Jing 3 pm 4} # Union operation print (set1.union (set2))
Pop removes the data and deletes it, which is equivalent to the operation in the stack, sorting and removing the top element set2= {2, 3, 4} set2.pop () print (set2)
# update requires two collections to update set1.update (set2) print (set1) based on the original
Summary
Assignment:
# find the sum of three sets of continuous natural numbers, and find the sum of 1 to 10, 20 to 30, 35 to 45 and def qiuhe (n d'%sum2 m): result=0 for item in range (npencil m): result+=item pass return result passsum1=qiuhe (10 d'%sum2 1) sum2=qiuhe (1) sum3=qiuhe (35 d'%sum2 45) print ('output 1 to 10 sum:% d'%sum1) print (' output 20 to 30 sum:% d'%sum2) print ('output 35 to 45 sum:% d'%sum3)
100 monks eat 100 steamed buns, the big monks eat three steamed buns, and the young monks eat one steamed bread. I would like to ask how many monks, big and small, def hscount ():''calculate how many monks there are assuming that the big monk and the young monk are 100: return:' for an in range (1100): if axiom 3 + (100Muth3) * (1gamma 3) = = 100: return (amae100) passrsobj=hscount () print ('number of big monks {}) Number of young monks {} '.format (rsobj [0], rsobj [1]))
# specify a list that contains the only number that has only appeared once, and write a program to find out the unique number list1= [1, set1 2, 3, 4, 5, 8, 8] set1=set (list1) # turns into a collection, removing the repetition # print (set1) for i in set1: list1.remove (I) passset2=set (list1) print (set1.difference (set2)).
The above is all the content of this article "what is a set in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!