[898] python get the intersection of two lists | union | difference

  1. Get the intersection of two lists:
# method one:
a=[2,3,4,5]
b=[2,5,8]
tmp =[val for val in a if val in b]print(tmp)
#[2,5]

# Method two is much faster than method one!
print list(set(a).intersection(set(b)))
  1. Get the union of two lists:
# method one:print(list(set(a+b)))

# Method two is much faster than method one!
print(list(set(a).union(set(b))))
  1. Get the difference of two lists:
# method one:
tmp =[val for val in b if val not in a] #what is in b but not in a
print(tmp)

# Method two is much faster than method one!
print list(set(b).difference(set(a))) #What is in b but not in a is very efficient!

python Set intersection, union, difference

s =set([3,5,9,10,20,40])      #Create a set of values
t =set([3,5,9,1,7,29,81])      #Create a set of values

a = t | s          #union of t and s,Equivalent to t.union(s)
b = t & s          #the intersection of t and s,Equivalent to t.intersection(s) 
c = t - s          #Difference set (items are in t but not in s),Equivalent to t.difference(s) 
d = t ^ s          #Symmetric difference set (items are in t or s, but will not appear in both),Equivalent to t.symmetric_difference(s)

Reference: https://www.cnblogs.com/jlf0103/p/8882896.html
https://www.cnblogs.com/jingtyu/p/7238743.html

Recommended Posts

[898] python get the intersection of two lists | union | difference
Two days of learning the basics of Python
I beg you, don’t write a loop to find the intersection, union and difference of two lists | pythonic tips
The difference between the syntax of java and python
The meaning and usage of lists in python
A brief summary of the difference between Python2 and Python3
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
The difference between Python extensions
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
The usage of wheel in python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
What is the use of Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Python implements alternate execution of two threads
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Python implementation of intersection and IOU tutorial
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
What is the id function of python
Where is the pip path of python3
The essence of Python language: Itertools library
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development
What is the function body of python
The specific method of python import library
Solve the conflict of multiple versions of python
What is the function of adb in python
Detailed explanation of the principle of Python super() method
Python realizes the development of student management system
Python implements the shuffling of the cards in Doudizhu
Solve the problem of python running startup error
Can the value of the python dictionary be modified?
Python implements the source code of the snake game
Detailed explanation of the usage of Python decimal module
How about learning python at the age of 27?
What is the difference between python and pycharm
The consequences of uninstalling python in ubuntu, very
Python writes the game implementation of fishing master