Sử dụng phương thức zip để liệt kê danh sách trừ trong Python. Bạn cũng phải sử dụng vòng lặp for để lặp qua đối tượng zip và trừ các phần tử của danh sách với nhau và lưu kết quả vào danh sách
Nếu bạn đang muốn tìm sự khác biệt giữa các danh sách thì hãy làm theo hướng dẫn này. –Sự khác biệt trong danh sách Python. mã ví dụ
Danh sách Python trừ ví dụ danh sách
Mã ví dụ đơn giản. Trước tiên, điều thực sự có nghĩa là tính toán một danh sách bằng cách trừ các phần tử tương ứng trong hai danh sách
Ví dụ: nếu các danh sách là [9,5,14] và [1,4,6], chúng tôi muốn kết quả là danh sách [8,1,8] bằng cách tính các chênh lệch 9–1,5–4,14–
list1 = [9, 5, 14]
list2 = [1, 4, 6]
def subtract[L1, L2]:
return [x1 - x2 for x1, x2 in zip[L1, L2]]
print[subtract[list1, list2]]
đầu ra
Một vi dụ khac
Danh sách Python trừ danh sách phần tử khôn ngoan [phép trừ thành phần. ]
list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
đầu ra. [8, 1, 8]
Hãy bình luận nếu bạn có bất kỳ nghi ngờ hoặc đề xuất nào về mã Danh sách Python này
Ghi chú. IDE. PyCharm 2021. 3. 3 [Phiên bản cộng đồng]
cửa sổ 10
Trăn 3. 10. 1
Tất cả các Ví dụ về Python đều nằm trong Python 3, vì vậy có thể nó khác với python 2 hoặc các phiên bản nâng cấp
Rohit
Bằng cấp về Khoa học Máy tính và Kỹ sư. Nhà phát triển ứng dụng và có kinh nghiệm về nhiều ngôn ngữ lập trình. Đam mê công nghệ & thích học hỏi kỹ thuật
Trong bài viết này, chúng ta sẽ học cách thực hiện phép trừ trên một danh sách trong Python. Chúng tôi cũng sẽ sử dụng một số hàm tích hợp và một số mã tùy chỉnh. Trước tiên chúng ta hãy xem nhanh danh sách và phép trừ danh sách trong Python là gì
Danh sách Python
Python có một kiểu dữ liệu tích hợp được gọi là danh sách. Nó giống như một tập hợp các mảng với các phương pháp khác nhau. Dữ liệu bên trong danh sách có thể thuộc bất kỳ loại nào, chẳng hạn như số nguyên, chuỗi hoặc giá trị float hoặc thậm chí là loại danh sách. Danh sách sử dụng các giá trị được phân tách bằng dấu phẩy trong dấu ngoặc vuông để lưu trữ dữ liệu. Danh sách có thể được xác định bằng cách sử dụng bất kỳ tên biến nào và sau đó gán các giá trị khác nhau cho danh sách trong dấu ngoặc vuông. Danh sách được sắp xếp, có thể thay đổi và cho phép các giá trị trùng lặp
list1 = ["Ram","Arun","Kiran"]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
Liệt kê phép trừ
Phép trừ danh sách sử dụng hai danh sách và trả về một danh sách mới sau phép trừ. Điều này tương tự như thực hiện phép trừ trong toán học. Nó tìm thấy sự khác biệt giữa từng phần tử tại mỗi chỉ mục từ hai danh sách đã cho
#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]
#output
list3 = [3,4,-1,3,-2]
New List = Elements of List1 - Elements of List2
Để thực hiện phép trừ danh sách, hai danh sách đầu vào phải có cùng độ dài và nó phải chứa các phần tử cùng loại i. e. cả hai danh sách chỉ được chứa các giá trị số. Ví dụ đã cho trừ các phần tử tại mỗi chỉ mục trong một danh sách khỏi danh sách khác
Thí dụ. Phép trừ liệt kê Sử dụng toán tử zip[] và "-"
Phương pháp này sử dụng
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]3 với list1 là
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]4 và list2 là
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]5 để trả về một đối tượng zip. Nó sử dụng vòng lặp for để lặp qua đối tượng zip và trừ các phần tử của danh sách với nhau và lưu kết quả vào một danh sách mới
#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
[3, 4, -1, 3, -2]
Phần kết luận
Trong bài viết này, chúng ta đã học cách tìm sự khác biệt giữa từng phần tử của hai danh sách bằng cách duyệt qua chúng bằng cách sử dụng đối tượng
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]6. Chúng tôi đã học cách thực hiện phép trừ danh sách trên từng phần tử
💡 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 có thể trừ. Ví dụ: trừ Danh sách chuỗi khỏi Danh sách số nguyên hoặc số float sẽ dẫn đến lỗi. Trong khi đó, việc trừ Danh sách số nguyên khỏi Danh sách số float hoặc ngược lại sẽ hoạt động
Bài viết này sử dụng hai [2] Danh sách, một Danh sách chứa 5 giá trị nguyên và 5 giá trị float khác
Ví dụ
- Đầu vào 1.
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
7 - Đầu vào 2.
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
8 - đầu ra.
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
9
💬 câu hỏi. Làm thế nào chúng ta sẽ viết mã để trừ hai danh sách?
Chúng ta 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à
00list1 = [9, 5, 14] list2 = [1, 4, 6] difference = [] zip_object = zip[list1, list2] for list1_i, list2_i in zip_object: difference.append[list1_i - list2_i] print[difference]
- Phương pháp 2. Sử dụng
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
0 - Phương pháp 3. Sử dụng
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
1 vàimport numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
2 - Phương pháp 4. Sử dụng một
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
3 - Phương pháp 5. Sử dụng Danh sách Hiểu và
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
4 - Phương pháp 6. Sử dụng vòng lặp
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
5
Phương pháp 1. Sử dụng Danh sách hiểu và zip[]
Ví dụ này sử dụng tính năng Hiểu danh sách và hàm
list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
00 để trừ hai [2] Danh sáchlist1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
1Đoạn mã trên khai báo hai [2]
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]7. Danh sách số nguyên và Danh sách số float. Chúng lần lượt lưu vào các biến
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8 và
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9
Dòng tiếp theo sử dụng
list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
50 thực hiện như sau- Hàm
00 được gọi và truyền hai [2] đối số. iterables. Trong trường hợp này,list1 = [9, 5, 14] list2 = [1, 4, 6] difference = [] zip_object = zip[list1, list2] for list1_i, list2_i in zip_object: difference.append[list1_i - list2_i] print[difference]
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
8 vàimport numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
9. Hàm này trả về một trình lặp đối tượng zip [ví dụ:.
54]list1 = [9, 5, 14] list2 = [1, 4, 6] difference = [] zip_object = zip[list1, list2] for list1_i, list2_i in zip_object: difference.append[list1_i - list2_i] print[difference]
- Tiếp theo, mỗi phần tử từ
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
8 [
56] được trừ khỏilist1 = [9, 5, 14] list2 = [1, 4, 6] difference = [] zip_object = zip[list1, list2] for list1_i, list2_i in zip_object: difference.append[list1_i - list2_i] print[difference]
57 [list1 = [9, 5, 14] list2 = [1, 4, 6] difference = [] zip_object = zip[list1, list2] for list1_i, list2_i in zip_object: difference.append[list1_i - list2_i] print[difference]
58] thông qua đối tượng zip được hiển thị ở trênlist1 = [9, 5, 14] list2 = [1, 4, 6] difference = [] zip_object = zip[list1, list2] for list1_i, list2_i in zip_object: difference.append[list1_i - list2_i] print[difference]
- Kết quả từ thao tác này được làm tròn xuống đến hai [2] chữ số thập phân
Các kết quả này lưu vào
list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
59 và xuất ra thiết bị đầu cuốilist1 = ["Ram","Arun","Kiran"]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
40💡Lưu ý. Để đảm bảo kết quả chỉ có hai [2] chữ số thập phân, hàm
list1 = ["Ram","Arun","Kiran"]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
41 đã được sử dụngGiới thiệu đơn giản về hiểu danh sách trong Python
Xem video này trên YouTube
Phương pháp 2. sử dụng np. trừ []
Ví dụ này sử dụng hàm
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]0 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 bị lỗi. Bấm vào đây nếu bạn cần hướng dẫn cài đặt
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]
Dòng đầu tiên của đoạn mã trên nhập thư viện NumPy để cung cấp các hàm
list1 = ["Ram","Arun","Kiran"]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
43 và import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]0
Hai [2] dòng sau đây tạo ra hai [2] Danh sách, Danh sách số nguyên và Danh sách số float. Các Danh sách này được chuyển đổi thành NumPy
list1 = ["Ram","Arun","Kiran"]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
45 và được lưu vào các biến import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8 và
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9, tương ứng
Nếu xuất ra thiết bị đầu cuối, nội dung của các
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]7 này sẽ xuất hiện như sau
list1 = ["Ram","Arun","Kiran"]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
49Tiếp theo, hàm
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]0 được gọi và truyền hai [2] mảng NumPy. Trong trường hợp này,
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8 và
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9
Phép toán trừ được thực hiện [
#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]
#output
list3 = [3,4,-1,3,-2]
New List = Elements of List1 - Elements of List2
83] trên import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]7 đã nói và kết quả được lưu vào
list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
59 và được xuất ra thiết bị đầu cuối#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]
#output
list3 = [3,4,-1,3,-2]
New List = Elements of List1 - Elements of List2
86Hướng dẫn NumPy - Mọi thứ bạn cần biết để bắt đầu
Xem video này trên YouTube
Phương pháp 3. sử dụng toán tử. phụ và bản đồ[]
Ví dụ này nhập
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]1 kết hợp với hàm
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]2 để trừ hai [2]
#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]
#output
list3 = [3,4,-1,3,-2]
New List = Elements of List1 - Elements of List2
89list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
5Dòng đầu tiên trong đoạn 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 thao tác Python tiêu chuẩn, chẳng hạn như cộng, trừ, nhân và nhiều hơn thế nữa
Hai [2] dòng sau đây tạo ra hai [2] Danh sách, Danh sách số nguyên và Danh sách số float. Các Danh sách này lần lượt được lưu vào các biến
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8 và
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9
Dòng tiếp theo gọi hàm
#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
82Bên trong
#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
82, hàm import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]2 được gọi và truyền ba [3] đối số. thao tác để thực hiện [
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]1], một lần lặp [
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8] và một lần lặp khác [
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9] trả về một đối tượng bản đồ
Nếu đối tượng bản đồ được xuất ra thiết bị đầu cuối, một đối tượng tương tự như bên dưới sẽ hiển thị
#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
88Sau đó, toán tử trừ được thực hiện và kết quả chuyển đổi thành Danh sách và xuất ra thiết bị đầu cuối
#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
89Làm chủ chức năng bản đồ Python [+Video]
Xem video này trên YouTube
Phương pháp 4. Sử dụng map[] và lambda
Ví dụ này sử dụng một
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]3 kết hợp với hàm
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]2 để trừ hai [2]
#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]
#output
list3 = [3,4,-1,3,-2]
New List = Elements of List1 - Elements of List2
89list1 = ["Ram","Arun","Kiran"]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
4Đoạn mã trên khai báo hai [2] Danh sách. Danh sách số nguyên và Danh sách số float. Chúng lần lượt lưu vào các biến
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8 và
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9
Sau đây gọi hàm
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]2. Hàm này được truyền ba [3] đối số, một
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]3, một lần lặp [
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8] và một lần lặp khác [
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9]. 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
#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
82Bên trong
#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
82, hàm import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]2 được gọi và truyền ba [3] đối số. thao tác để thực hiện [
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]1], một lần lặp [
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8] và một lần lặp khác [
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9] trả về một đối tượng bản đồ
Nếu đối tượng bản đồ được xuất ra thiết bị đầu cuối, một đối tượng tương tự như bên dưới sẽ hiển thị
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]85
Toán tử trừ sau đó được thực hiện, được chuyển đổi thành Danh sách, được lưu vào
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]86 và xuất ra thiết bị đầu cuối
#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
89Hãy chơi Finxter - Hàm Lambda trong Python
Xem video này trên YouTube
Phương pháp 5. Sử dụng Danh sách hiểu và liệt kê []
Ví dụ này sử dụng chức năng Hiểu danh sách và hàm
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]4 để trừ hai [2]
#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]
#output
list3 = [3,4,-1,3,-2]
New List = Elements of List1 - Elements of List2
89#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]
#output
list3 = [3,4,-1,3,-2]
New List = Elements of List1 - Elements of List2
8Đoạn mã trên khai báo hai [2] Danh sách. Danh sách số nguyên và Danh sách số float. Chúng lần lượt lưu vào các biến
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8 và
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9
Dòng sau sử dụng tính năng Hiểu danh sách để trừ hai [2] Danh sách. Toán tử này sử dụng hàm
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]4 và được truyền một [1] đối số.
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8. Kết quả được lưu vào
list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
59và được xuất ra thiết bị đầu cuối💡 Lưu ý. Hàm
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]4 lấy, ví dụ: Danh sách, Tuple, v.v. , như một đối số. Điều này sau đó sẽ thêm một bộ đếm và sử dụng bộ đếm này làm khóa cho đối tượng đã nói
Python enumerate[] — Hướng dẫn đơn giản
Xem video này trên YouTube
Phương pháp 6. Sử dụng vòng lặp
Ví dụ này sử dụng vòng lặp
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]5 để trừ hai [2]
#input
list1 = [4,8,1,9,5]
list2 = [1,4,2,6,7]
#output
list3 = [3,4,-1,3,-2]
New List = Elements of List1 - Elements of List2
89#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
8Đoạn mã trên khai báo ba [2]
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]7. Danh sách số nguyên, Danh sách số float và danh sách rỗng. Chúng lần lượt lưu vào các biến
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8,
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]9 và
list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
59Tiếp theo một vòng lặp
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]5 được khởi tạo. Vòng lặp này lặp qua từng phần tử của
import numpy as np list_ints = np.array[[2028, 3913, 2816, 4301, 1853]] list_floats = np.array[[335.36, 442.88, 200.54, 327.11, 410.09]] new_list = np.subtract[list_ints, list_floats] print[new_list]8, thực hiện phép toán trừ và nối kết quả vào
list1 = [9, 5, 14]
list2 = [1, 4, 6]
difference = []
zip_object = zip[list1, list2]
for list1_i, list2_i in zip_object:
difference.append[list1_i - list2_i]
print[difference]
59. Kết quả được xuất ra thiết bị đầu cuối#takes two input lists
list1 = [4, 8, 1, 9, 5]
list2 = [1, 4, 2, 6, 7]
#empty list
sub_list = []
#two lists are passed to zip
zip_object = zip[list1, list2]
#loop to find diff of each element
for list1_i, list2_i in zip_object:
sub_list.append[list1_i - list2_i]
print[sub_list]
89Tóm lược
Bài viết này đã cung cấp năm [5] cách để trừ hai Danh sách để chọn cách phù hợp nhất cho yêu cầu viết mã của bạn