In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.