Hướng dẫn can you subtract two lists in python? - bạn có thể trừ hai danh sách trong python không?
Nhiều giải pháp đã được đề xuất. Show
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)
Sử dụng 6 rõ ràng là nhanh nhất. Đáng ngạc nhiên, 7 là chậm nhất. Nó chỉ ra rằng chi phí đầu tiên chuyển đổi danh sách 8 và 9 thành mảng 7 là một nút cổ chai vượt xa mọi hiệu quả đạt được từ vector hóa. 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. 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
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:
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:
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:
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
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 PythonTrong 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. Python3Cá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 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? 0 1 2 3Output: 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ừ.Python3Cá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 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 2 3Chúng ta có thể trừ 2 danh sách trong Python không? 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 PythonBạn có thể tổng hợp hai danh sách trong Python không? Python3Cá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 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 2 3Output: 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 52 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 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 75 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 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; 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 PythonHà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. Python3Input 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 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 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: 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 PythonCá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. Python3Cá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; 2 3Output: 5Chú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ừ.Use a list comprehension and set to Find the Difference Between Two Lists in Python. In this method, we convert the lists into sets explicitly and then simply reduce one from the other using the subtract operator.
Bạn có thể tổng hợp hai danh sách trong Python không?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.. A zip() function is used in the sum() function to group list elements using index-wise lists.
Bạn có thể trừ trong Python?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.addition and subtraction operators perform similarly to mathematics. In fact, you can use the Python programming language as a calculator. |