Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find union, intersection, subtraction of Python code list

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the "Python code list how to find union, intersection, difference" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

I. list union

The function of merging two lists is realized. At the same time, it supports the use of a filter condition function, according to which all elements in the list are union, and the set is the original elements of the two lists.

The code snippet read in this article is from 30-seconds-of-python.

1. Union_by

Def union_by (a, b, fn): _ a = set (map (fn, a)) return list (set (a + [item for item in b if fn (item) not in _ a]) # EXAMPLESfrom math import floorunion_by ([2.1,2.3], floor) # [2.1,1.2]

The union_by function receives two lists and a filter condition function. After applying the provided function to each element in the two lists, a new list is returned containing all the non-repeating elements that exist in the two lists.

The map function, which has been explained before, returns an iterator that applies the transformation function fn to all list elements.

Set is a special data type of Python, which is an unordered set of non-repeating elements. This function uses the set type directly to eliminate duplicate elements in the list.

Special: when the filter condition function is lamda XRX, the function is converted to directly finding the union of the two lists.

2. list to find the intersection

The function of finding the intersection of two lists is realized. At the same time, it supports the use of a filter condition function, according to which all elements in the list intersect, and the intersection set is the original element of the two lists.

1. Intersection_bydef intersection_by (a, b, fn): _ b = set (map (fn, b)) return [item for item in an if fn (item) in _ b] # EXAMPLESfrom math import floorintersection_by ([2.1,1.2], [2.3,3.4], floor) # [2.1]

The intersection_by function receives two lists and a filter condition function. After applying the provided function to each element in the two lists, returns a list of elements that exist in the two lists. The function creates a collection by applying fn to each element in b, and then uses list deduction and fn on a to retain only values that exist in both lists.

The map function, which has been explained before, returns an iterator that applies the transformation function fn to all list elements.

Set is a special data type of Python, which is an unordered set of non-repeating elements. This function uses the set type directly to eliminate duplicate elements in the list.

Special: when the filter condition function is lamda XRIX, the function is converted to directly finding the intersection of the two lists.

3. List the difference set

Implement three different ways of list subtraction. It is the difference of the list directly, the difference_by of all the elements in the list according to the condition function, and the symmetric difference symmetric_difference_by of all the elements in the list according to the condition function.

1. Differencedef difference (a, b): _ b = set (b) return [item for item in an if item not in _ b] # EXAMPLESdifference ([1,2,3], [1,2,4]) # [3]

The difference function returns the difference between two iterable objects. This function creates a collection _ b from b and then uses a list derivation on a, leaving only the values not contained in _ b. There is a sequential relationship between an and b in this function, and amurb deletes the data contained in b from a.

Set is a special data type of Python, which is an unordered set of non-repeating elements. This function uses the set type directly to eliminate duplicate elements in the list.

2. Difference_by

Def difference_by (a, b, fn): _ b = set (map (fn, b)) return [item for item in an if fn (item) not in _ b] # EXAMPLESfrom math import floordifference_by ([2.1,1.2], [2.3,3.4], floor) # [1.2] difference_by ([{'xrooms: 2}, {' xtrees: 1}], [{'xcake: 1}] Lambda v: v ['x']) # [{x: 2}]

The difference_by function receives two lists and a filter condition function. After applying the provided function to each element in the two lists, the difference between the two original lists is returned. The function creates a collection by applying fn to each element in b, and then combines fn with list deduction on a, leaving only the values not contained in the previously created collection _ b.

Special: when the filter condition function is lamda XRIX, the function is converted to directly finding the difference between the two lists.

3. Symmetric_difference_bydef symmetric_difference_by (a, b, fn): _ a, _ b = set (map (fn, a)), set (map (fn, b)) return [item for item in an if fn (item) not in _ b] + [item for item in b if fn (item) not in _ a] # EXAMPLESfrom math import floorsymmetric_difference_by ([2.1,1.2], [2.3,3.4], floor) # [1.2,3.4]

The symmetric_difference_by function, after applying the provided function to each list element in the two lists, returns the symmetric difference between the two original lists. The function creates two sets _ an and _ b by applying fn to each element in each list, and then uses list understanding and fn on each element to retain only the values that are not included in the other sets created previously (in a, not in _ b; in b, not in _ a. ).

In particular: when the filter condition function is lamda XRX, the function is converted to directly finding the symmetry difference between the two lists.

This is the end of the content of "how to find union, intersection and difference in Python code list". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report