Python Set Method Summary

Source: Internet
Author: User
Tags new set

1. Add an item at the tail

Add (...)

This have no effect if the element is already present

>>> a = set ([1,2,3,4,5])

>>> A.add(' Shaw ')

>>> A

Set ([1, 2, 3, 4, 5, ' Shaw '])

2 . Empties all elements in the collection

Clear (...)

Remove all elements from the This set

>>>a = Set ([1, 2, 3, 4, 5, ' Shaw '])

>>>a.Clear()

>>>a

Set ([])

3. Returns a new collection, but the elements in the new collection, a has,b does not

Difference (...)

>>> a = set ([1, 2, 3, 4, 5, ' Shaw '])

>>> B = Set ([2,3,5, ' Sam '])

>>> A.difference(b)

Set ([1, 4, ' Shaw '])

4. Delete from set a , elements owned by both collection a and set b

Difference_update (...)

>>> a = set ([1, 2, 3, 4, 5, ' Shaw '])

>>> B = Set ([2,3,5, ' Sam '])

>>> A.difference_update(b)

>>> A

Set ([1, 4, ' Shaw '])

>>> b

Set ([2, 3, 5, ' Sam '])

5. Delete The specified element in the set (even if the element does not exist with this set, no error will be given)

Discard (...)

The Remove an element from a set if it is amember. f The element is not a member, does nothing.

>>> a = set ([1, 2, 3, 4, 5, ' Shaw '])

>>> A.Discard(' 5 ')

>>> A

Set ([1, 2, 3, 4, 5, ' Shaw '])

>>> A.discard (' Sam ')

>>> A

Set ([1, 2, 3, 4, 5, ' Shaw '])

6. Returns a new collection with a new collection element consisting of the intersection of set a and set B

Intersection (...)

Return the intersection of or more Setsas a new set.

>>> a = set ([1, 2, 3, 4, 5, ' Shaw '])

>>> B = Set ([2,3,5, ' Sam '])

>>> a.intersection (b) equivalent to A & B

Set ([2, 3, 5])

7. remove elements from set a thatare outside the intersection of a and b

Intersection_update (...)

>>> a = set ([1, 2, 3, 4, 5, ' Shaw '])

>>> B = Set ([2,3,5, ' Sam '])

>>> A.intersection_update (b)

>>> A

Set ([2, 3, 5])

8. Determine if two sets have intersections, and if so, return True, otherwise False

Isdisjoint (...)

>>> a = set ([1, 2, 3, 4, 5, ' Shaw '])

>>> B = Set ([2,3,5, ' Sam '])

>>> A.isdisjoint (b)

False

>>> C = Set ([9, ' alices '])

>>> A.isdisjoint (c)

True

9 . Tests whether the elements in one collection are in another element (that is, whether test set b is a subset of collection a )

Issubset (...)

Report whether another set contains the this set

Note:

Issuperset (...)

Report whether this set contains another set

>>> a = set ([1, 2, 3, 4, 5, ' Shaw '])

>>> B = Set ([2,3,5, ' Sam '])

>>> C = Set ([5, ' Shaw '])

>>> B.issubset (a)

False

>>> C.issubset (a) equivalent to C <= a

True

Delete an element (the first item is deleted by default) and if the element to be deleted does not exist, it is reported keyerror

Pop (...)

>>>a = Set ([1, 2, 3, 4, 5, ' Shaw '])

>>>a.pop ()

1

>>>a

Set ([2,3, 4, 5, ' Shaw '])

Delete the specified element, and if the element to be deleted does not exist, it is reported keyerror

Remove (...)

>>> a = set ([1, 2, 3, 4, 5, ' Shaw '])

>>> A.remove (1)

>>> A

Set ([2, 3, 4, 5, ' Shaw '])

>>> a.remove (' Shaw ')

>>> A

Set ([2, 3, 4, 5])

Symmetric difference set (this element is in a and not in b , and b has a elements not in the

Symmetric_difference (...)

>>> a = set (' ABCdef ')

>>> B = Set (' Abcklyt ')

>>> A.symmetric_difference (b)

Set ([' E ', ' t ', ' f ', ' y ', ' k ', ' l ', ' d '])

>>> a ^ b

Set ([' E ', ' t ', ' f ', ' y ', ' k ', ' l ', ' d '])

Take a set (returns a new set containing each element in s and t )

Union (...)

>>> a = set (' ACDF ')

>>> B = Set (' Adkh ')

>>> A.union (b)

Set ([' A ', ' C ', ' d ', ' f ', ' h ', ' K '])

>>> A | B

Set ([' A ', ' C ', ' d ', ' f ', ' h ', ' K '])

Add an element to collection a (when another collection is added, duplicate elements are removed)

Update (...)

>>> a = set (' ACDF ')

>>> B = Set (' Adkh ')

>>> A.update (b)

>>> A

Set ([' A ', ' C ', ' d ', ' f ', ' h ', ' K '])

>>> a = set (' ACDF ')

>>> B = Set (' Adkh ')

>>> a |= b

>>> A

Set ([' A ', ' C ', ' d ', ' f ', ' h ', ' K '])

>>> a= set (' Shaw ')

>>> a.update (' Bnga ')

>>> A

Set ([' A ', ' B ', ' g ', ' h ', ' n ', ' s ', ' W ')


This article is from the "Shaw blog" blog, please be sure to keep this source http://opsedu.blog.51cto.com/9265055/1762866

Python Set Method Summary

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.