Python3 set method function example, python3set

Source: Internet
Author: User

Python3 set method function example, python3set

Add (add element)

Name = set (['Tom ', 'Lucy', 'ben']) name. add ('juny') print (name) # output: {'Lucy ', 'juny', 'ben', 'Tom '}

Clear (clear all elements)

Name = set (['Tom ', 'Lucy', 'ben']) name. clear () print (name) # output: set ()

Copy (copy set)

Name = set (['Tom ', 'Lucy', 'ben']) new_name = name. copy () print (new_name) # output: {'Tom ', 'Lucy', 'ben '}

Difference (returns two or more sets of different elements and generates a new set)

A = set ([2, 3, 4, 5]) B = set ([3, 4]) C = set ([2]) n = n1.difference (n2, n3) print (n) # output: {5}
# Return the elements that are not in Set B and set C in set A and generate A new set.

Difference_update (delete the elements in set A that exist in Set B .)

A = set ([2, 3, 4, 5]) B = set ([4, 5]) A. difference_update (B) print (A) # output: {2, 3}

Discard (remove element)

N = set ([2, 3, 4]) n. discard (3) print (n) # output: {2, 4}

Intersection (take the intersection and generate a new set)

N1 = set ([2, 3, 4, 5]) n2 = set ([4, 5, 6, 7]) n = n1.intersection (n2) print (n) # output: {4, 5}

Intersection_update (take the intersection and modify the original set)

N1 = set ([2, 3, 4, 5]) n2 = set ([4, 5, 6, 7]) n1.intersection _ update (n2) print (n1) # output: {4, 5}

Isdisjoint (determines the intersection. False is returned; True is returned)

N1 = set ([2, 3, 4, 5]) n2 = set ([4, 5, 6, 7]) print (n1.isdisjoint (n2) # output: False

Issubset)

A = set ([2, 3]) B = set ([2, 3, 4, 5]) print (A. issubset (B) # output: True # A is A subset of B

Issuperset (determine parent set)

A = set ([2, 3]) B = set ([2, 3, 4, 5]) print (B. issuperset (A) # output: True # B is the parent set of

Pop (remove an element randomly)

N = set ([2, 3, 4, 5]) n1 = n. pop () print (n, n1) # output: {3, 4, 5} 2

Remove (remove a specified element)

N = set ([2, 3, 4, 5]) n. remove (2) print (n) # output: {3, 4, 5}

Symmetric_difference (intersection and generate a new set)

A = set ([2, 3, 4, 5]) B = set ([4, 5, 6, 7]) print (. symmetric_difference (B) # output: {2, 3, 6, 7}

Symmetric_difference_update (take the intersection and change the original set)

A = set ([2, 3, 4, 5]) B = set ([4, 5, 6, 7]). symmetric_difference_update (B) print (A) # output: {2, 3, 6, 7}

Union (obtains the union set and generates a new set)

A = set ([2, 3, 4, 5]) B = set ([4, 5, 6, 7]) print (. union (B) # output: {2, 3, 4, 5, 6, 7}

Update (take the Union set and change the original set)

A = set ([2, 3, 4, 5]) B = set ([4, 5, 6, 7]). update (B) print (A) # output: {2, 3, 4, 5, 6, 7}

 

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.