Hướng dẫn how do you subtract two elements in python? - làm thế nào để bạn trừ hai phần tử trong python?

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)
5 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)
6 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)
7 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)
8 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)
6 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)

Nhà văn kỹ thuật của công ty nội bộ cho các chương trình phần mềm khác nhau như Navision và Microsoft CRM Corporate Huấn luyện viên (nhân viên của 30+)How 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.

Chúng ta có thể trừ 2 danh sách trong Python không?

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 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ừ.

  • Biểu tượng trừ trong Python là gì?
  • 2. Toán tử trừ: Trong Python, - là toán tử trừ. Nó được sử dụng để trừ đi giá trị thứ hai khỏi giá trị thứ nhất.
  • 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.

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à,

Trong phương pháp này, chúng tôi sẽ chuyển hai danh sách đầu vào cho hàm zip. Sau đó, lặp lại trên đối tượng zip bằng cách sử dụng cho vòng lặp. Trên mỗi lần lặp, chương trình sẽ lấy một yếu tố từ List1 và List2, trừ chúng và nối kết quả vào danh sách khác.

Ví dụ 1:

# 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]

Thực hiện phép trừ bằng cách sử dụng danh sách hiểu

Một cách khác để trừ hai danh sách là bằng cách sử dụng danh sách hiểu. Đối với điều này, bạn cần phải đi qua các danh sách và thực hiện phép trừ từng phần tử như trong đoạn mã bên dưới.

#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]

Sự khác biệt của hai danh sách sử dụng mảng numpy

Hai phương pháp trước đó yêu cầu truyền tải trong toàn bộ danh sách. Một trong những phương pháp đơn giản nhất là chuyển đổi hai danh sách thành một mảng. Ở đây, hàm np.array () chuyển đổi hai danh sách thành mảng và sau đó sử dụng toán tử trừ.

#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]

Nếu bạn có bất kỳ câu hỏi nào liên quan đến bài viết này, hãy liên hệ với chúng tôi. Phản hồi của bạn quan trọng rất nhiều. Xem thêm hướng dẫn Python

Xây dựng vấn đề và tổng quan về giải pháp

Bài viết này sẽ chỉ cho bạn cách trừ hai danh sách trong Python.

Lưu ý: Trước khi trừ các danh sách này, hãy đảm bảo mỗi danh sách có cùng độ dài và chứa dữ liệu trừ. Ví dụ, trừ một danh sách các chuỗi từ danh sách các số nguyên hoặc phao sẽ dẫn đến một lỗi. Trong khi việc trừ một danh sách các số nguyên từ danh sách các phao, hoặc ngược lại sẽ hoạt động.Note: Before subtracting these Lists, ensure each List is of the same length and contains subtractable data. For example, subtracting a List of strings from a List of integers or floats will result in an error. Whereas subtracting a List of integers from a List of floats, or vice-versa will work.

Bài viết này sử dụng hai (2) danh sách, một danh sách chứa 5 giá trị số nguyên và 5 giá trị float còn lại.

Ví dụ:

  • Đầu vào 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]
    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]
    0
  • Đầu vào 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]
    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]
    1
  • Đầu ra:
    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]
    2

💬 Câu hỏi: Làm thế nào chúng ta sẽ viết mã để trừ hai danh sách?Question: How would we write code to subtract two lists?

Chúng tôi có thể hoàn thành nhiệm vụ này bằng một trong các tùy chọn sau:

  • Phương pháp 1: Sử dụng danh sách hiểu và
    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
    1: Use List Comprehension and
    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
  • Phương pháp 2: Sử dụng
    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]
    4
    2: Use
    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]
    4
  • Phương pháp 3: Sử dụng
    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 và
    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]
    6
    3: Use
    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 and
    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]
    6
  • Phương pháp 4: Sử dụng
    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]
    7
    : Use 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]
    7
  • Phương pháp 5: Sử dụng danh sách hiểu và
    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]
    8
    : Use List Comprehension and
    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]
    8
  • Phương pháp 6: Sử dụng
    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]
    9Loop
    : Use 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]
    9loop

Phương pháp 1: Sử dụng danh sách hiểu và zip ()

Ví dụ này sử dụng danh sách hiểu và hàm

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 để trừ hai (2) danh sách.

list_ints   = [2028, 3913, 2816, 4301, 1853]
list_floats = [335.36, 442.88, 200.54, 327.11, 410.09]

new_list = [round(x-y,2) for x, y in zip(list_ints, list_floats)]
print(new_list)

Mã trên tuyên bố hai (2)

# 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)
1: Danh sách các số nguyên và danh sách phao. Chúng lưu vào các biến
# 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)
2 và
# 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)
3, tương ứng.

Dòng tiếp theo sử dụ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)
4 trong đó:

  • Hàm
    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 được gọi và chuyển hai (2) đối số: tererables. Trong trường hợp này,
    # 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)
    
    2 và
    # 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)
    
    3. Hàm này trả về một trình lặp đối tượng zip (ví dụ:
    # 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).
  • Tiếp theo, mỗi phần tử từ
    # 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)
    
    2 (
    [5, -3, -2, 1]
    0) được trừ khỏi
    [5, -3, -2, 1]
    1 (
    [5, -3, -2, 1]
    2) thông qua đối tượng zip được hiển thị ở trên
  • Các kết quả từ hoạt động này được làm tròn xuống hai (2) vị trí thập phân.

Những kết quả này tiết kiệm đến

[5, -3, -2, 1]
3 và đầu ra vào thiết bị đầu cuối.

[5, -3, -2, 1]
4

💡note: Để đảm bảo kết quả chỉ có hai (2) vị trí thập phân, hàm

[5, -3, -2, 1]
5 đã được sử dụng.Note: To ensure the results have only two (2) decimal places, the
[5, -3, -2, 1]
5 function was used.

Giới thiệu đơn giản về danh sách hiểu trong Python


Phương pháp 2: Sử dụng NP.SubTract ()

Ví dụ này sử dụng hàm

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]
4 từ thư viện Numpy để trừ hai (2) danh sách.

Thư viện Numpy sẽ cần được cài đặt để chạy mã này không có lỗi. Nhấn vào đây nếu bạn yêu cầu hướng dẫn cài đặt.

%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)
0

Dòng đầu tiên của mã trên nhập thư viện Numpy để tạo các hàm

[5, -3, -2, 1]
7 và
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]
4 có sẵn.

Hai (2) dòng sau đây tạo ra hai (2) danh sách, một danh sách các số nguyên và danh sách các phao. Các danh sách này được chuyển đổi thành

[5, -3, -2, 1]
9 numpy và được lưu vào các biến
# 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)
2 và
# 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)
3, tương ứng.

Nếu đầu ra vào thiết bị đầu cuối, nội dung của 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)
1 này xuất hiện như sau:

#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)
3

Tiếp theo, hàm

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]
4 được gọi và chuyển hai (2) mảng numpy. Trong trường hợp này,
# 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)
2 và
# 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)
3.

Hoạt động trừ được thực hiện (

#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)
7) trên
# 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)
1 đã nói và kết quả tiết kiệm cho
[5, -3, -2, 1]
3 và là đầu ra cho thiết bị đầu cuối.

[5, -3, -2]
0

Hướng dẫn Numpy - Mọi thứ bạn cần biết để bắt đầu


Phương pháp 3: Sử dụng toán tử.sub và map ()

Ví dụ này nhập

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 kết hợp với hàm
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]
6 để trừ hai (2)
# 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)
1.

%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)
1

Dòng đầu tiên trong mã trên nhập thư viện toán tử. Thư viện này cung cấp các cách thay thế để thực hiện các hoạt động Python tiêu chuẩn, chẳng hạn như thêm, trừ, nhân và nhiều, nhiều hơn nữa!

Hai (2) dòng sau đây tạo ra hai (2) danh sách, một danh sách các số nguyên và danh sách các phao. Các danh sách này được lưu vào các biến

# 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)
2 và
# 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)
3, tương ứng.

Dòng tiếp theo gọi hàm

[5, -3, -2]
6.

Bên trong

[5, -3, -2]
6, hàm
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]
6 được gọi và thông qua ba (3) đối số: Hoạt động để thực hiện (
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), một điều không thể sử dụng đượ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)
2) và một đối tượng khá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)
3) trả về một đối tượng bản đồ.

Nếu đối tượng bản đồ được xuất vào thiết bị đầu cuối, một đối tượng tương tự như bên dưới sẽ hiển thị.

#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)
2

Toán tử trừ sau đó được thực hiện và kết quả chuyển đổi thành danh sách và đầu ra thành thiết bị đầu cuối.

#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)
3

Làm chủ hàm bản đồ Python [+Video]


Phương pháp 4: Sử dụng bản đồ () và Lambda

Ví dụ này sử dụng

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]
7 kết hợp với hàm
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]
6 để trừ hai (2)
# 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)
1.

%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)
2

Mã trên tuyên bố hai (2) danh sách: Danh sách các số nguyên và danh sách các phao. Chúng lưu vào các biến

# 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)
2 và
# 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)
3, tương ứng.

Sau đây gọi hàm

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]
6. Hàm này được thông qua ba (3) đối số, một
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]
7, một điều đáng tin cậy (
# 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)
2) và một đối số khá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)
3). Kết quả trả về một đối tượng bản đồ có thể lặp lại.

Dòng tiếp theo gọi hàm

[5, -3, -2]
6.

Bên trong

[5, -3, -2]
6, hàm
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]
6 được gọi và thông qua ba (3) đối số: Hoạt động để thực hiện (
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), một điều không thể sử dụng đượ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)
2) và một đối tượng khá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)
3) trả về một đối tượng bản đồ.

Nếu đối tượng bản đồ được xuất vào thiết bị đầu cuối, một đối tượng tương tự như bên dưới sẽ hiển thị.

[-2  4  4 -7 -1]
9

Toán tử trừ sau đó được thực hiện, chuyển đổi thành một danh sách, được lưu thành

list_ints   = [2028, 3913, 2816, 4301, 1853]
list_floats = [335.36, 442.88, 200.54, 327.11, 410.09]

new_list = [round(x-y,2) for x, y in zip(list_ints, list_floats)]
print(new_list)
0 và xuất vào thiết bị đầu cuối.

#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)
3

Hãy chơi Finxter - Chức năng Lambda trong Python


Phương pháp 5: Sử dụng danh sách hiểu và liệt kê ()

Ví dụ này sử dụng hàm hiểu danh sách và hàm

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]
8 để trừ hai (2)
# 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)
1.

%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)
3

Mã trên tuyên bố hai (2) danh sách: Danh sách các số nguyên và danh sách các phao. Chúng lưu vào các biến

# 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)
2 và
# 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)
3, tương ứng.

Dòng sau sử dụng danh sách hiểu để trừ hai (2) danh sách. Toán tử này sử dụng hàm

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]
8 và được truyền một (1) đối số:
# 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)
2. Các kết quả tiết kiệm đến
list_ints   = [2028, 3913, 2816, 4301, 1853]
list_floats = [335.36, 442.88, 200.54, 327.11, 410.09]

new_list = [round(x-y,2) for x, y in zip(list_ints, list_floats)]
print(new_list)
8and là đầu ra cho thiết bị đầu cuối.

Lưu ý: Hàm

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]
8 lấy, ví dụ, một danh sách, một tuple, v.v., như một đối số. Điều này sau đó sẽ thêm một bộ đếm và sử dụng điều này làm chìa khóa cho đối tượng nói trên.: The
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]
8 function takes, for example, a List, a Tuple, etc., as an argument. This will then add a counter and use this as a key for the said object.

Python Enumerate () - Hướng dẫn đơn giản


Phương pháp 6: Sử dụng cho vòng lặp

Ví dụ này sử dụng vòng lặp

%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)
00 để trừ hai (2)
# 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)
1.

%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)
4

Mã trên tuyên bố ba (2)

# 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)
1: Danh sách các số nguyên, danh sách các phao và một danh sách trống. Chúng lưu vào các biến
# 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)
2,
# 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)
3 và
[5, -3, -2, 1]
3, tương ứng.

Tiếp theo một

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]
9Loop được khởi tạo. Vòng lặp này lặp qua từng phần tử của
# 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)
2, thực hiện thao tác trừ và nối các kết quả vào
[5, -3, -2, 1]
3. Kết quả là đầu ra cho thiết bị đầu cuối.

#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)
3


Bản tóm tắt

Bài viết này đã cung cấp năm (5) cách để trừ hai danh sách để chọn phù hợp nhất cho các yêu cầu mã hóa của bạn.

Chúc may mắn và mã hóa hạnh phúc!


Lập trình viên hài hước - Blockchain

Hướng dẫn how do you subtract two elements in python? - làm thế nào để bạn trừ hai phần tử trong python?
Các blockchains giống như những cái móc vật lộn, trong đó nó cực kỳ tuyệt vời khi bạn gặp phải một vấn đề mà họ là giải pháp phù hợp, nhưng nó xảy ra quá hiếm khi trong cuộc sống thực. Nguồn - XKCDsource – xkcd

Hướng dẫn how do you subtract two elements in python? - làm thế nào để bạn trừ hai phần tử trong python?

Ở trường đại học, tôi tìm thấy tình yêu viết và mã hóa của mình. Cả hai trong số đó tôi đã có thể sử dụng trong sự nghiệp của mình.

Trong 15 năm qua, tôi đã giữ một số vị trí như:

Nhà văn kỹ thuật của công ty nội bộ cho các chương trình phần mềm khác nhau như Navision và Microsoft CRM Corporate Huấn luyện viên (nhân viên của 30+)
Corporate Trainer (staff of 30+)
Programming Instructor
Implementation Specialist for Navision and Microsoft CRM
Senior PHP Coder

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ừ.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.

Biểu tượng trừ trong Python là gì?

2. Toán tử trừ: Trong Python, - là toán tử trừ.Nó được sử dụng để trừ đi giá trị thứ hai khỏi giá trị thứ nhất. is the subtraction operator. It is used to subtract the second value from the first value.