Nhiều giải pháp đã được đề xuất.
Nếu tốc độ được quan tâm, thì đây là đánh giá các giải pháp khác nhau liên quan đến tốc độ [từ nhanh nhất đến chậm nhất]
import timeit
import operator
a = [2,2,2]
b = [1,1,1] # we want to obtain c = [2,2,2] - [1,1,1] = [1,1,1
%timeit map[operator.sub, a, b]
176 ns ± 7.18 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit map[int.__sub__, a, b]
179 ns ± 4.95 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit map[lambda x,y: x-y, a,b]
189 ns ± 8.1 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit [a_i - b_i for a_i, b_i in zip[a, b]]
421 ns ± 18.4 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit [x - b[i] for i, x in enumerate[a]]
452 ns ± 17.2 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each
%timeit [a[i] - b[i] for i in range[len[a]]]
530 ns ± 16.7 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit list[map[lambda x, y: x - y, a, b]]
546 ns ± 16.1 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
%timeit np.subtract[a,b]
2.68 µs ± 80.9 ns per loop [mean ± std. dev. of 7 runs, 100000 loops each]
%timeit list[np.array[a] - np.array[b]]
2.82 µs ± 113 ns per loop [mean ± std. dev. of 7 runs, 100000 loops each]
%timeit np.matrix[a] - np.matrix[b]
12.3 µs ± 437 ns per loop [mean ± std. dev. of 7 runs, 100000 loops each]
Sử dụng
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
6 rõ ràng là nhanh nhất. Đáng ngạc nhiên, %timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
7 là chậm nhất. Nó chỉ ra rằng chi phí đầu tiên chuyển đổi danh sách %timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
8 và %timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
9 thành mảng %timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
7 là một nút cổ chai vượt xa mọi hiệu quả đạt được từ vector hóa.%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]39
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]41How to Subtract two lists in Python. Before performing list subtraction, keep in mind that both lists should be of the same length and all the elements should be of the same datatype.
[5, -3, -2, 1]
7Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]44
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]
Phương pháp 6: Sử dụng symmetric_difference để tìm sự khác biệt giữa hai danh sách trong Python
- Các phần tử trong tập đầu tiên hoặc bộ thứ hai được trả về bằng kỹ thuật symmetric_difference []. Giao lộ, không giống như các mục được chia sẻ của hai bộ, không được trả lại bởi kỹ thuật này.
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]
75Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]
2Input: list1 = [10, 15, 20, 25, 30, 35, 40] list2 = [25, 40, 35] Output: [10, 20, 30, 15] Explanation: resultant list = list1 - list2
2Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]
78______9280280
7[5, -3, -2, 1]
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]
2Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]
47Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]
84
Chúng ta có thể trừ 2 danh sách trong Python không?
Phương pháp 3: Sử dụng danh sách hiểu và đặt để tìm sự khác biệt giữa hai danh sách trong Python. Trong phương pháp này, chúng tôi chuyển đổi danh sách thành các bộ một cách rõ ràng và sau đó chỉ cần giảm cái này từ mẫu kia bằng toán tử trừ.
Bạn có thể tổng hợp hai danh sách trong Python không?
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]
OUTPUT:
[5, -3, -2, 1]
Hàm SUM [] được sử dụng để thêm hai danh sách bằng số chỉ mục của các phần tử danh sách được nhóm theo hàm zip []. Hàm zip [] được sử dụng trong hàm SUM [] để nhóm các thành phần danh sách bằng cách sử dụng danh sách theo chỉ mục.
Bạn có thể trừ trong Python?
#create and initialize two lists list1 = [9,1,3] list2 = [4,4,5] #perform subtraction and store the result in "difference" difference = [List1[i]-List2[i] for i in range[min[len[list1], len[List2]]]] #print the difference of two lists print[difference]
Output:
[5, -3, -2]
Trong Python, các toán tử bổ sung và trừ thực hiện tương tự như toán học. Trên thực tế, bạn có thể sử dụng ngôn ngữ lập trình Python làm máy tính.
Trong hướng dẫn này, bạn sẽ học cách trừ hai danh sách trong Python. Trước khi thực hiện phép trừ danh sách, hãy nhớ rằng cả hai danh sách phải có cùng độ dài và tất cả các yếu tố phải thuộc cùng một kiểu dữ liệu.
#create and initialize two lists list1 = [2,3,9,-4,7] list2 = [4,-1,5,3,8] #convert the two lists into arrays and store the difference difference = np.array[list1]-np.array[list2] #print the difference of two lists print[difference]
Output:
[-2 4 4 -7 -1]
Chẳng hạn, giả sử bạn có hai danh sách và bạn muốn thực hiện phép trừ giữa hai danh sách này, tức là,
Có nhiều cách khác nhau trong đó sự khác biệt giữa hai danh sách có thể được tạo ra. Trong bài viết này, chúng ta sẽ thấy các cách khác nhau để có được sự khác biệt giữa hai danh sách có thể được thực hiện bằng Python.
Examples:
Input: list1 = [10, 15, 20, 25, 30, 35, 40] list2 = [25, 40, 35] Output: [10, 20, 30, 15] Explanation: resultant list = list1 - list2
& nbsp; Lưu ý: Khi bạn có nhiều yếu tố giống nhau thì điều này sẽ không hoạt động. Trong trường hợp đó, mã này sẽ chỉ cần xóa các yếu tố tương tự. Trong trường hợp đó, bạn có thể duy trì số lượng của từng phần tử trong cả hai danh sách.When you have multiple same elements then this would not work. In that case, this code will simply remove the same elements.
In that case, you can maintain a count of each element in both lists.
Phương pháp 1: Sử dụng trên mạng trong phạm vi để tìm sự khác biệt giữa hai danh sách trong PythonUse “in” to Find the Difference Between Two Lists in Python
Trong ví dụ này, chúng tôi đang sử dụng Loop và Python trong từ khóa để tìm sự khác biệt giữa hai danh sách trong Python.
Python3
Các
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]8
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]3
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]0
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]6
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]4
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]7
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]75
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input: list1 = [10, 15, 20, 25, 30, 35, 40] list2 = [25, 40, 35] Output: [10, 20, 30, 15] Explanation: resultant list = list1 - list22
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]78______9280280
[5, -3, -2, 1]
7Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]47
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]84
Chúng ta có thể trừ 2 danh sách trong Python không?
[5, -3, -2]
0[5, -3, -2]
1[5, -3, -2]
2[5, -3, -2]
3Output:
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
0Phương pháp 3: Sử dụng danh sách hiểu và đặt để tìm sự khác biệt giữa hai danh sách trong Python. Trong phương pháp này, chúng tôi chuyển đổi danh sách thành các bộ một cách rõ ràng và sau đó chỉ cần giảm cái này từ mẫu kia bằng toán tử trừ.
Python3
Các
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]8
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]3
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]0
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]6
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]4
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]7
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]75
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input: list1 = [10, 15, 20, 25, 30, 35, 40] list2 = [25, 40, 35] Output: [10, 20, 30, 15] Explanation: resultant list = list1 - list22
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]78______9280280
[5, -3, -2, 1]
7Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]47
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]84
[5, -3, -2]
2[5, -3, -2]
3Chúng ta có thể trừ 2 danh sách trong Python không?
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
0Phương pháp 3: Sử dụng danh sách hiểu và đặt để tìm sự khác biệt giữa hai danh sách trong Python. Trong phương pháp này, chúng tôi chuyển đổi danh sách thành các bộ một cách rõ ràng và sau đó chỉ cần giảm cái này từ mẫu kia bằng toán tử trừ. Use a list comprehension and set to Find the Difference Between Two Lists in Python
Bạn có thể tổng hợp hai danh sách trong Python không?
Python3
Các
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]8
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]3
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]0
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]6
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]4
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]7
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]75
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input: list1 = [10, 15, 20, 25, 30, 35, 40] list2 = [25, 40, 35] Output: [10, 20, 30, 15] Explanation: resultant list = list1 - list22
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]78______9280280
[5, -3, -2, 1]
7Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]47
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]84
[5, -3, -2]
2[5, -3, -2]
3Output:
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
0Chúng ta có thể trừ 2 danh sách trong Python không? Without using the set[]
Phương pháp 3: Sử dụng danh sách hiểu và đặt để tìm sự khác biệt giữa hai danh sách trong Python. Trong phương pháp này, chúng tôi chuyển đổi danh sách thành các bộ một cách rõ ràng và sau đó chỉ cần giảm cái này từ mẫu kia bằng toán tử trừ.
Python3
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
52 %timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
53#create and initialize two lists list1 = [9,1,3] list2 = [4,4,5] #perform subtraction and store the result in "difference" difference = [List1[i]-List2[i] for i in range[min[len[list1], len[List2]]]] #print the difference of two lists print[difference]4
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
55______22#create and initialize two lists list1 = [9,1,3] list2 = [4,4,5] #perform subtraction and store the result in "difference" difference = [List1[i]-List2[i] for i in range[min[len[list1], len[List2]]]] #print the difference of two lists print[difference]4
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
75 %timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
76Các
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]8
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]3
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]0
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]6
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]4
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]7
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]03
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]05
[5, -3, -2]
2Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]07
Đầu ra: & nbsp;
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
0Phương pháp 5: Sử dụng Numpy để tìm sự khác biệt giữa hai danh sách trong Python Use Numpy to Find the Difference Between Two Lists in Python
Hàm numpy.concatenate [] kết hợp một chuỗi các mảng dọc theo một trục hiện có.numpy.concatenate[] function concatenate a sequence of arrays along an existing axis.
Python3
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]08
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]09
Các
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]8
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]12
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]0
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]6
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]4
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]26
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]36
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]38
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]39
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]41
[5, -3, -2, 1]
7Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]44
[5, -3, -2]
2Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]46
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]47
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]48
Output:
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
0Phương pháp 6: Sử dụng symmetric_difference để tìm sự khác biệt giữa hai danh sách trong Python Use symmetric_difference to Find the Difference Between Two Lists in Python
Các phần tử trong tập đầu tiên hoặc bộ thứ hai được trả về bằng kỹ thuật symmetric_difference []. Giao lộ, không giống như các mục được chia sẻ của hai bộ, không được trả lại bởi kỹ thuật này.
Python3
Các
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]8
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]3
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]0
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]6
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]5
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]4
# Create and initialize two lists list1 = [9,1,3,7] list2 = [4,4,5,6] #initialize a variable which will store the difference of two lists result = [] for i, j in zip[list1,list2]: result.append[i - j] print[result]7
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]03
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]2
Input list 1 = [7,6,2,4,-2,8,9] Input list 2 = [2,9,-3,0,9,5,6] Output: Result = [7,6,2,4,-2,8,9] - [2,9,-3,0,9,5,6] = [5,-3,5,4,-11,3,3]05
Đầu ra: & nbsp;
[5, -3, -2]
2[5, -3, -2]
3Output:
%timeit a = np.array[[2,2,2]]; b=np.array[[1,1,1]]
1.55 µs ± 54.9 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
a = np.array[[2,2,2]]
b = np.array[[1,1,1]]
%timeit a - b
417 ns ± 12.8 ns per loop [mean ± std. dev. of 7 runs, 1000000 loops each]
5