Detailed introduction of python-set collection class method

高洛峰
Release: 2017-03-20 11:11:45
Original
1344 people have browsed it

 s1=set([11,22,33,44,'Tom','tony',11,77,2.5,]) returns {11,22,33,44, 'Tom', 'tony', 77,2.5} (Note: What is returned is not a dictionary, it just tells you that this set contains these elements, so the order of the result elements returned each time may be different)

 s2=set([11,22,33,44,'Tom','tony',11,55,66,]) returns {11,22,33,44,'Tom','tony ', 55,66} (Note: What is returned is not a dictionary, it just tells you that this set contains these elements, so the order of the result elements returned each time may be different)

add(. ..)
 Add an element to a set.
 This has no effect if the element is already present. (If the element already exists, adding has no effect. This means that set is a collection without duplicate elements.)

For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

s1.add('jimi')

print(s1)

 Result: {33, 'tony', 2.5, 'jimi', 11, 44, 77, 22, 'Tom'}( Note: set is an unordered set, and the order of the results may be different each time. )

difference(...)
Returnthe difference of two or more. sets as anewset. (Find the main difference set and generate a new set)
  (i.e.allelements that are in this set but not the others.) (Note: s1.difference(s2) means to find the elements that are in s1 but not in s2, and generate a new set. s2.difference(s1) means to find out the elements that are in s2 but not in s1 elements and generate a new set)

For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

  s2=set([11,22,33,44,'Tom','tony',11,55,66,])

  s3=s1.difference(s2)

   s4=s2.difference(s1)

   print(s3)

  print(s4)

  Result: {2.5, 77}

  { 66, 55}

difference_update(...)
 Remove all elements of another set from this set. (Remove all elements of another set from this set. Note: s1.difference_update(s2) means to remove the elements in s1 that are common to both, and retain the elements that are not common. It is a modification of s1 rather than generating a new list. s2.difference_update(s1) means to remove the elements in s2 that are common to both, and retain the elements that are not common. It is a modification of s2 rather than generating a new list. )

 For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

  s2=set([11 ,22,33,44,'Tom','tony',11,55,66,])

  s1.difference_update(s2)

  print(s1)

Result: {2.5, 77}

discard(...)
Remove an element from a set if it is a member. (Remove members (elements) in the set)
If the element is not a member, do nothing. (If there is no such member in the set, no operation will be performed, and no error will be reported, which is different from Remove.)

 For example: s1=set([11 ,22,33,44,'Tom','tony',11,77,2.5,])

  s1.discard(2.5)

Result: {33, 'Tom', 11, 44, 77, 'tony', 22}

intersection(...)
Return the intersection of two sets as a new set. (Generate the intersection of the two sets and generate a new list)
 (i.e. all elements that are in both sets.)

 For example: s1=set([11,22,33,44,'Tom','tony',11,77, 2.5,])

  s2=set([11,22,33,44,'Tom','tony',11,55,66,])

  s5=s1.intersection (s2)

  s6=s2.intersection(s1)

   print(s5)

   print(s6)

  Result: {33, 'Tom ', 11, 44, 22, 'tony'}

  {33, 11, 44, 'tony', 'Tom', 22}

intersection_update(...)
Update a set with the intersection of itself and another. (The function is the same as intersection(...), but s1.intersection(s2) modifies the original set s1 when executed and does not generate a new one. Set)

For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set( [11,22,33,44,'Tom','tony',11,55,66,])

  s1.intersection(s2)

  s2.intersection(s1)

print(s1)

print(s2)

Result: {33, 'Tom', 11, 44, 22, 'tony'}

{33, 11, 44, 'tony', 'Tom', 22}

isdisjoint(...)
Return True if two sets have anullintersection. (Determine whether the two sets have an intersection, if so, return False, if not, return True)

For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

s2=set([100,50,500 ,])

  print(s1.isdisjoint(s2))

 Result: True

issubset(...)
 Report whether another set contains this set. (To determine whether all elements in a set are in another set, s1.issubset(s2) means that every element in s1 is in s2. Pay attention to the difference from s1.issuperset(s2). )

For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

s2=set([11,22,33 ,44,'Tom','tony',11,])

  s3=([11,22,33,44,'Tom','tony',11,55,66])

s1.issubset(s2)

s1.issubset(s3)

print(s1.issubset(s2))

print(s1.issubset(s3 ))

Result: True

False

issuperset(...)
Report whether this set contains another set. (Judge whether all elements in a set In another set, s1.issubset(s2) means that every element in s2 is in s1. Note the difference with s1.issubset(s2) )

For example: s1=set( [11,22,33,44,'Tom','tony',11,77,2.5,])

  s2=set([11,22,33,44,'Tom','tony ',11,])

  s3=([11,22,33,44,'Tom','tony',11,55,66])

  s1.issuperset(s2 )

   s1.issuperset(s3)

   print(s1.issuperset(s2))

   print(s1.issuperset(s3))

  Result :True

##pop(...)  

 Remove and return an arbitrary set element.(Randomly delete an element)
  Raises KeyError if the set is empty.( If the set is empty, KeyError will be prompted when pop is executed)

For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

s1.pop()

Result: {33, 'Tom', 2.5, 11, 44, 77, 22}

remove(...)

Remove an element from a set; it must be a member. If there is no such member, keyError will be prompted, which is different from discard. )

 For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

  s1.remove(2.5)

  s1.remove('jimi')

 Result: {33, 'Tom', 'tony', 11, 44, 77, 22}

  KeyError: ' jimi'

symmetric_difference(...)

 Return the symmetric difference of two sets as a new set. (Generate a new list, this list is a set of non-duplicate elements in the two lists, s1 .symmetric_difference(s2) represents a set of elements in s1 that are not in s2 and elements in s2 that are not in s1)


 (i.e. all elements that are in exactly one of the sets.)

 For example: s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

  s2=set([11,22,33, 44,'Tom','tony',11,55,66,])

  s4=s2.symmetric_difference(s1)

  print(s4)

 Result: {2.5, 66, 77, 55}

symmetric_difference_update(...)

 Update a set with the symmetric difference of itself and another. (Modify a list, there are no duplicate elements in the two lists The set of s1.symmetric_difference(s2) represents the set of elements in s1 that are not in s2 and elements in s2 that are not in s1)


For example: s1=set([11,22,33,44 ,'Tom','tony',11,77,2.5,])

  s2=set([11,22,33,44,'Tom','tony',11,55,66, ])

      s1.symmetric_difference_update(s2)

   print(s1)

  Result: {2.5, 66, 77, 55}

union(. ..)

Return the union of sets as a new set. (Generate a new set. The changed list is a set of all members (elements) of the two lists. s1.union(s2) means that it contains s1, s2 A new set of all elements)

 (i.e. all elements that are in either set.)

 For example: s1=set([11,22,33,44,'Tom','tony',11 ,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s3= s1.union(s2)

  print(s3)

 Result: {33, 2.5, 66, 'Tom', 11, 44, 77, 55, 22, 'tony'}

update(...)

Update a set with the union of itself and others. (Update one set to another set, s1.update(s2) means putting all elements in s2 into In s1, complete the update and modification of s1)


For example: s1=set([100,50,])

s2=set([11,22,33,44,'Tom ','tony',11,55,66,])

   s1.update(s2)

   print(s1)

 Result: {'tony', 33, 66, 100, 'Tom', 11, 44, 50, 22, 55}

The above is the detailed content of Detailed introduction of python-set collection class method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!