Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the function to get the intersection, union and difference of two sets by python

Shulou Source: shulou.com Published: 2022-06-01 02:17:44 10月04日 Update

This article is about how python uses functions to get the intersection, union, and subtraction of two sets. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Get the difference () function of the intersection, union, and difference of two sets

What is the difference set:

The sets of an and b, consisting of elements that belong to an and do not belong to b, are called the difference between a set and b set.

The function of the difference () function: returns the difference of a collection, that is, the returned collection elements are contained in the first collection, but not in the second collection.

The usage of the difference () function: a_set.difference (b_set); b_set is the set that the current set needs to be compared; the return value is the difference of the original set acting on the contrast set. (i.e. the difference between a_set and b_set)

Examples are as follows:

Names_set_01 = {'Neo',' Lily', 'Jack'} names_set_02 = {' Jack', 'Adem',' Albina'} names_diff = names_set_01.difference (names_set_02) print (names_diff) # the result is as follows: # > > {'Lily',' Neo'} intersection () function

What is intersection?

Two sets, an and b, respectively have the same set of elements, which is called the intersection of a set and b set.

The function of the intersection () function returns elements contained in two or more collections. (that is, intersection)

The use of the intersection () function: a_set.inersection (baked set...); b_set... One or more sets that need to be compared for the current set; the return value is the intersection of the original set acting on the contrast set.

Examples are as follows:

Names_set_01 = {'Neo',' Lily', 'Jack',' Adem'} names_set_02 = {'Jack',' Adem', 'Albina'} names_set_03 = {' Apollo', 'Ben',' Adem'} names_inter = names_set_01.intersection (names_set_02, names_set_03) print (names_inter) # the execution result is as follows: # > {'Adem'} union () function

What is union?

All elements in the an and b sets (excluding duplicates) are the union of the a set and the b set.

The function of the union () function: returns the union of multiple sets, that is, elements that contain all sets (duplicate elements appear only once)

The use of the union () function: a_set.union (baked set...); b_set... One or more sets that need to be compared for the current set; the return value is the union of the original set acting on the contrast set.

Examples are as follows:

Names_set_01 = {'Neo',' Lily', 'Jack',' Adem'} names_set_02 = {'Jack',' Adem', 'Albina'} names_set_03 = {' Apollo', 'Ben',' Adem'} names_union = names_set_01.union (names_set_02, names_set_03) print (names_union) # the execution result is as follows: # > {'Lily',' Neo', 'Albina',' Apollo' 'Jack', 'Ben',' Adem'} Thank you for your reading! This is the end of this article on "how to use python to get two sets of intersection, union, and difference sets". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Function difference two element intersection function multiple more example result cooperation content article good practical same article look knowledge reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Xiaomi Apple Docker