"Python Full stack Notes" 03 [Module II] 16-17 OCT set set, trinocular operation

Source: Internet
Author: User
Tags new set set set shallow copy union of sets

Set Set

Set-unordered collections of unique elements

Create a set/an empty set
#Create a new setSet1 = {}Print(Type (SET1))#result = <class ' set>Set2=set ()Print(Type (Set2))#Create an empty set, result = <class ' Set ' >#as the object is iterable, we can create a set with list, tuple, dictionary etc.List1 = [1, 2, 3]set2=Set (List1)Print(Set2, type (Set2))#{1, 2, 3} <class ' Set ' >Tuple1= (1, 2, 3) Set2=Set (tuple1)Print(Set2, type (Set2))#{1, 2, 3} <class ' Set ' >Dict1= {'K1': 1,'K2': 2}set2=Set (Dict1)Print(Set2, type (Set2))#{' K1 ', ' K2 '} <class ' Set ' >#Check if below is workable to create an empty setSet3 = {}Print(Type (SET3))#type result = <class ' dict ' > Failed#So the variable_name = {} only works for creating a new dictionary

#to add a new element, we can use Add ()#As set only contains different elements, if we add the same element multiple times, only one would remainSet1= {1, 2, 3,}set1.add (4) Set1.add (4) Set1.add (4)Print(Set1)#result = {1, 2, 3, 4}#To clear a setset1.clear ()Print(Set1)#= = Set ()#A.difference (b), elements in A and not in BSet1 = {1, 2, 3, 5, 8,}set2= {0, 1, 2, 3,}ret1=set1.difference (Set2)Print(RET1)#+ = {8, 5}Ret2 =set2.difference (Set1)Print(Ret2)#+ = {0}#difference () function does the set, if we want to change it, we can use Difference_update ()Set0 = {-1,}set0.difference_update (Set2)Print(set0)#Discard () to remove the element, if not a member, does nothingSet0 = {1, 2, 3,}set0.discard (1)Print(set0) Set0.discard (-1)Print(set0)#both print out: {2, 3}, no error msg#If we use Remove (), when the target element isn't a member, there ' ll be error#Set0.remove ( -1)#print (set0)" "Traceback (most recent call last): Set () File "d:/naomipyer/1016/1017_set_functions.py", line, in <module>{8 , 5} set0.remove ( -1) {0}keyerror:-1" "#intersection (), gets the shared infoSet1 = {1, 2, 3, 5, 8,}set2= {0, 1, 2, 3,}ret3=set1.intersection (Set2)Print(RET3)#+ = {1, 2, 3}#a.intersection_update (b) Update a with the intersection of A and bSet1 = {1, 2, 3, 5, 8,}set2= {0, 1, 2, 3,}set1.intersection_update (Set2)Print(Set1)#= = {1, 2, 3,}#Isdisjoint () returns True if the sets has no intersectionSet1 = {1, 2, 3,}set2={0,}ret1=set1.isdisjoint (Set2)Print(RET1)#= TrueSet1= {1, 2, 3,}set2= {1,}ret1=set1.isdisjoint (Set2)Print(RET1)#= False#Issubset () report whether another set contains this setSet1 = {1,}set2= {0, 1, 2, 3,}ret1=Set1.issubset (Set2)Print(RET1)#= = True, Set1 is the subset of Set2#Issuperset () is the oppositeRet1 =Set1.issuperset (Set2) Ret2=Set2.issuperset (Set1)Print(RET1)#FalsePrint(Ret2)#True#pop () remove and return an arbitrary set element.#raises keyerror If the set is emptySet1 = {1, 2, 3, 4, 6,}ret1=Set1.pop ()Print(Set1)#+ = {2, 3, 4, 6}Print(RET1)#= 1Set1 =set () Ret1=Set1.pop ()Print(Set1)Print(RET1)#keyerror: ' Pop from a empty set '-like remove (), raises keyerror#symmetric_difference ()#returns the symmetric difference of the sets as a new oneSet1 = {0, 1, 2,}set2= {-1, 0, 3}ret1=set1.symmetric_difference (Set2)Print(RET1)#+ = {1, 2, 3,-1}#symmetric_difference_update ()#Update Set1 with the resultSet1 = {0, 1, 2,}set2= {-1, 0, 3}set1.symmetric_difference_update (Set2)Print(Set1)#+ = {1, 2, 3,-1}#Union () returns the union of sets as a new setSet1 = {0, 1, 2,}set2= {-1, 0, 3}ret1=set1.union (Set2)Print(RET1)#+ = {0, 1, 2, 3,-1}#Update () updates the set with the Union of itself and the other setSet1 = {0, 1, 2,}set2= {-1, 0, 3}set1.update (Set2)Print(Set1)#+ = {0, 1, 2, 3,-1}

With regard to the Set method, intersection () is the intersection of two sets, difference () is a complement, and Union () is set.

There is an update in the method, which is updated directly on the original set.

Three mesh operation | ternary operation

If condition:

Block1

Else

Block2

Name = value1 if condition else:value2

Name = if condition? Value1:value2

Shallow copy VS depth copy

When STR is created once, it cannot be modified and the data modification is actually created again.

The list lists the location of the next element after it is created

int and str assignments, copy and Deepcopy, they point to the same memory address

Import Copy

N1 = 123

N2 = N1

N3 = Copy.copy (N1)

N4 = Copy.deepcopy (n2)

For other data types such as list, tuple, dict, set, etc., the memory address is not the same at copy time.

1. Assigning values

Create a variable that points to the original memory address

2. Shallow copy and deep copy

Shallow copy creates only the extra first layer of data in memory

Deepcopy was created once in addition to the last layer

1 ImportCopy2 3 4N1 = N1 = {5     'language':'python',6     'IDE':'Pycharm',7     'Operating System': ['Linux','Windows']8 }9 TenN2 =copy.copy (N1) OneN3 =copy.deepcopy (N1) A  - Print(ID (N1))#1195332519496 - Print(ID (n2))#1195332981832 the Print(ID (N3))#1195332937352 - #All ID (s) is different

Compare the points of copy and Deepcopy

"Python Full stack Notes" 03 [Module II] 16-17 OCT set set, trinocular operation

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.