Set минус set python

Set минус set python

Sets are mutable unordered collections of unique elements. Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.

Sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

Sets are implemented using dictionaries. They cannot contain mutable elements such as lists or dictionaries. However, they can contain immutable collections.

Constructors¶

set() Returns a set type initialized from iterable. <> set comprehension Returns a set based on existing iterables. literal syntax Initializes a new instance of the set type.

Methods¶

Adding Elements¶

Deleting¶

discard Removes an element from the set. remove Removes an element from the set (raises KeyError if not found). pop Removes and returns an arbitrary element from the set. clear Removes all elements from the set.

Information¶

issuperset Returns a Boolean stating whether the set contains the specified set or iterable. issubset Returns a Boolean stating whether the set is contained in the specified set or iterable. isdisjoint Returns a Boolean stating whether the set contents do not overlap with the specified set or iterable.

Читайте также:  Комментарии одной строки css

Set Operations¶

difference Returns a new set with elements in the set that are not in the specified iterables. intersection Returns a new set with elements common to the set and the specified iterables. symmetric_difference Returns a new set with elements in either the set or the specified iterable but not both. union Returns a new set with elements from the set and the specified iterables.

Set Operations Assignment¶

difference_update Updates the set, removing elements found in the specified iterables. intersection_update Updates the set, keeping only elements found in it and the specified iterables. symmetric_difference_update Updates the set, keeping only elements found in either set or the specified iterable, but not in both.

Copying¶

Set Operators¶

Adding Elements¶

Relational Operators¶

== (is equal) Returns a Boolean stating whether the set has the same elements as the other set. != (is not equal) Returns a Boolean stating whether the set has different elements as the other set. = (issuperset) Returns a Boolean stating whether the set contains the other set. > (issuperset proper) Returns a Boolean stating whether the set contains the other set and that the sets are not equal.

Set Operations¶

— (difference) Returns a new set with elements in the set that are not in the other set. & (intersection) Returns a new set with elements common to the set and the other set. ^ (symmetric_difference) Returns a new set with elements in either the set or the other set but not both. | (union) Returns a new set with elements from the set and the other set.

Set Operations Assignment¶

-= (difference_update) Updates the set, removing elements found in the other set. &= (intersection_update) Updates the set, keeping only elements found in it and the other set. ^= (symmetric_difference_update) Updates the set, keeping only elements found in either set or the other set, but not in both.

Functions¶

len Returns an int type specifying number of elements in the collection. min Returns the smallest item from a collection. max Returns the largest item in an iterable or the largest of two or more arguments. sum Returns a total of the items contained in the iterable object. sorted Returns a sorted list from the iterable. reversed Returns a reverse iterator over a sequence. all Returns a Boolean value that indicates whether the collection contains only values that evaluate to True. any Returns a Boolean value that indicates whether the collection contains any values that evaluate to True. enumerate Returns an enumerate object. zip Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.

© Copyright 2015, Jakub Przywóski. Revision 9a3b94e7 .

Versions latest Downloads pdf htmlzip epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.

Источник

Set Difference in Python – With Examples

In this tutorial, we will look at how to compute the set difference in Python with the help of some examples.

What is Set Difference?

Set difference in Python

The set difference operation between two sets, for example A — B , gives us the elements of set A which are not in set B. Let’s look at an example.

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Example showing the set difference operation A - B.

You can see that the resulting set from the A — B operation contains only the elements of set A which are not in set B.

Note that the set difference operation is not commutative. That is, A — B is not the same as B — A which results in a set containing the elements of B which are not in A. For example, computing B — A in the above example results in –

Example showing the set difference operation B-A.

Thus, the set differences A — B and B — A are different from one another and may or may not be equal.

Set Difference in Python

Python comes with a built-in set data structure to implement a set. It also has a number of additional functions to help you with common operations on sets such as union, intersection, difference, etc.

Difference of two sets

To get the difference between two sets in Python, you can use the set difference() function. The following is the syntax:

# set difference operation a - b a.difference(b)

It returns a new set containing the elements of a which are not in b.

# create two sets a = b = # set difference a - b a.difference(b)

We get the resulting set as as 1 is the only element in set a which is not present in set b.

Using the subtraction «-» operator for difference sets

Alternatively, you can also use the subtraction operator — to compute set differences in Python. For example –

# alternative method print(a - b)

We get the same result as above.

Note that the — operator for set difference only works on sets where as you can pass other iterables like lists, tuples, etc. to the set difference() function.

Set difference of more than two sets

You can also get the difference between more than two sets. You can use the following syntax.

# difference of more than two sets, for example, a,b,c,d a.difference(b,c,d)

Pass the sets (apart from the first one) as arguments to the difference() function. It returns a set containing the elements in set a which are not present in any of the passed sets. Let’s look at an example.

# create four sets a = b = c = d = # elements of set a not in b, c, and d a.difference(b,c,d)

We get the resulting set as since 5 and 8 are the only elements in set a which are not present in the sets b, c, and d.

You can also use the — operator for set differences involving more than two sets.

# alternative method print(a-b-c-d)

We get the same result as above (note that sets are not ordered).

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Источник

Оцените статью