Bạn chỉ có thể sử dụng danh sách hiểu biết:
my_list = [1, 2, 3, 4, 5] my_new_list = [i * 5 for i in my_list] >>> print(my_new_list) [5, 10, 15, 20, 25]Lưu ý rằng việc hiểu danh sách nói chung là một cách hiệu quả hơn để thực hiện vòng lặp my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 0:
my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25]Thay thế, đây là một giải pháp sử dụng gói Pandas phổ biến:
import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64Hoặc, nếu bạn chỉ muốn danh sách:
>>> (s * 5).tolist() [5, 10, 15, 20, 25]Cuối cùng, người ta có thể sử dụng my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 1, mặc dù điều này thường được cau mày.
my_new_list = map(lambda x: x * 5, my_list)Sử dụng my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 1, tuy nhiên, thường kém hiệu quả. Mỗi bình luận từ Shadowranger về câu trả lời đã xóa cho câu hỏi này:
Lý do "không ai" sử dụng nó là, nói chung, đó là một sự bi quan hiệu suất. Lần duy nhất đáng để xem xét my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 1 trong CPython là nếu bạn đang sử dụng chức năng tích hợp được triển khai trong C làm chức năng ánh xạ; Nếu không, my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 1 sẽ chạy bằng hoặc chậm hơn so với ListComp hoặc GenExpr của Pythonic (cũng rõ ràng hơn về việc họ là người tạo ra những người lười biếng hay háo hức my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 5; trong my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 5). Nếu bạn đang sử dụng my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 1 với chức năng my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 9, hãy dừng lại, bạn đang làm sai.
Và một trong những bình luận khác của anh ấy được đăng cho câu trả lời này:
Xin đừng dạy mọi người sử dụng my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 1 với my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 9; Ngay lập tức bạn cần một my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 9, bạn sẽ tốt hơn với danh sách hiểu hoặc biểu thức máy phát. Nếu bạn thông minh, bạn có thể làm cho my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 1 hoạt động mà không cần my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 9s nhiều, ví dụ: Trong trường hợp này, import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 5, mặc dù trong trường hợp cụ thể này, nhờ một số tối ưu hóa trong trình thông dịch mã byte cho toán học import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 6 đơn giản, import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 7 nhanh hơn, cũng như đơn giản hơn và đơn giản hơn.
Cho một danh sách, in giá trị thu được sau khi nhân tất cả các số trong danh sách. & NBSP;
Examples:
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 Input : list1 = [3, 2, 4] Output : 24Phương pháp 1: Truyền tảiTraversal
Khởi tạo giá trị của sản phẩm thành 1 (không phải 0 là 0 nhân với bất cứ thứ gì trả về 0). Traverse cho đến khi kết thúc danh sách, nhân mỗi số với sản phẩm. Giá trị được lưu trữ trong sản phẩm ở cuối sẽ cung cấp cho bạn câu trả lời cuối cùng của bạn.
Dưới đây là việc thực hiện Python của phương pháp trên: & nbsp; & nbsp;
Python
import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 8 import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 9
>>> (s * 5).tolist() [5, 10, 15, 20, 25] 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 1>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 >>> (s * 5).tolist() [5, 10, 15, 20, 25] 3
>>> (s * 5).tolist() [5, 10, 15, 20, 25] 0____10 >>> (s * 5).tolist() [5, 10, 15, 20, 25] 6>>> (s * 5).tolist() [5, 10, 15, 20, 25] 7 >>> (s * 5).tolist() [5, 10, 15, 20, 25] 8
>>> (s * 5).tolist() [5, 10, 15, 20, 25] 9>>> (s * 5).tolist() [5, 10, 15, 20, 25] 1>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 >>> (s * 5).tolist() [5, 10, 15, 20, 25] 1my_new_list = map(lambda x: x * 5, my_list) 3 my_new_list = map(lambda x: x * 5, my_list) 4
>>> (s * 5).tolist() [5, 10, 15, 20, 25] 0my_new_list = map(lambda x: x * 5, my_list) 6 my_new_list = map(lambda x: x * 5, my_list) 7
my_new_list = map(lambda x: x * 5, my_list) 8>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 7>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0__5555552553Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 222
Input : list1 = [3, 2, 4] Output : 24 6Input : list1 = [3, 2, 4] Output : 24 7
Input : list1 = [3, 2, 4] Output : 24 6Input : list1 = [3, 2, 4] Output : 24 9
Phương pháp 2: Sử dụng numpy.prod ()Using numpy.prod()
Chúng ta có thể sử dụng numpy.prod () từ nhập khẩu numpy để có được sự nhân của tất cả các số trong danh sách. Nó trả về một số nguyên hoặc giá trị float tùy thuộc vào kết quả nhân.
Dưới đây là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;
Python3
6 24 0 6 24 1
my_new_list = map(lambda x: x * 5, my_list) 8>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 7>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0__5555552553Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 222
Phương pháp 2: Sử dụng numpy.prod ()
Chúng ta có thể sử dụng numpy.prod () từ nhập khẩu numpy để có được sự nhân của tất cả các số trong danh sách. Nó trả về một số nguyên hoặc giá trị float tùy thuộc vào kết quả nhân.
Input : list1 = [3, 2, 4] Output : 24 66 247
Input : list1 = [3, 2, 4] Output : 24 66 249
Output:
Dưới đây là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;Using lambda function: Using numpy.array
6 24 0 6 24 1“return” statement, it always contains an expression that is returned. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions. The reduce() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list.
Dưới đây là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;
Python3
6 24 0 6 24 1
my_new_list = map(lambda x: x * 5, my_list) 8>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 7>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0__5555552553Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 222
Phương pháp 2: Sử dụng numpy.prod ()
Chúng ta có thể sử dụng numpy.prod () từ nhập khẩu numpy để có được sự nhân của tất cả các số trong danh sách. Nó trả về một số nguyên hoặc giá trị float tùy thuộc vào kết quả nhân.
Input : list1 = [3, 2, 4] Output : 24 66 247
Dưới đây là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;
6 24 0 6 24 1Using prod function of math library: Using math.prod
6 240>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 6 242
Dưới đây là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;
Python3
6 24 0 6 24 1
my_new_list = map(lambda x: x * 5, my_list) 8>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 7>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0__5555552553Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 222
Phương pháp 2: Sử dụng numpy.prod ()
Chúng ta có thể sử dụng numpy.prod () từ nhập khẩu numpy để có được sự nhân của tất cả các số trong danh sách. Nó trả về một số nguyên hoặc giá trị float tùy thuộc vào kết quả nhân.
Input : list1 = [3, 2, 4] Output : 24 66 247
Input : list1 = [3, 2, 4] Output : 24 66 249
Output:
Dưới đây là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;Using mul() function of operator module.
6 24 0 6 24 1
Python3
6 240>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 6 242
my_new_list = map(lambda x: x * 5, my_list) 8>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 7>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0__5555552553Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 222
Phương pháp 2: Sử dụng numpy.prod ()
Chúng ta có thể sử dụng numpy.prod () từ nhập khẩu numpy để có được sự nhân của tất cả các số trong danh sách. Nó trả về một số nguyên hoặc giá trị float tùy thuộc vào kết quả nhân.
Input : list1 = [3, 2, 4] Output : 24 6my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 97
Dưới đây là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp; Using traversal by index
Python3
6 24 0 6 24 1
>>> (s * 5).tolist() [5, 10, 15, 20, 25] 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 1>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 >>> (s * 5).tolist() [5, 10, 15, 20, 25] 3
>>> (s * 5).tolist() [5, 10, 15, 20, 25] 0____10 >>> (s * 5).tolist() [5, 10, 15, 20, 25] 6>>> (s * 5).tolist() [5, 10, 15, 20, 25] 7 >>> (s * 5).tolist() [5, 10, 15, 20, 25] 8
>>> (s * 5).tolist() [5, 10, 15, 20, 25] 9>>> (s * 5).tolist() [5, 10, 15, 20, 25] 1>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 >>> (s * 5).tolist() [5, 10, 15, 20, 25] 1my_new_list = map(lambda x: x * 5, my_list) 3 my_new_list = map(lambda x: x * 5, my_list) 4
>>> (s * 5).tolist() [5, 10, 15, 20, 25] 0my_new_list = map(lambda x: x * 5, my_list) 6 my_new_list = map(lambda x: x * 5, my_list) 7
my_new_list = map(lambda x: x * 5, my_list) 8>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 7>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0__5555552553Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 222
Input : list1 = [3, 2, 4] Output : 24 6Input : list1 = [3, 2, 4] Output : 24 7
Input : list1 = [3, 2, 4] Output : 24 6Input : list1 = [3, 2, 4] Output : 24 9
Phương pháp 7: Sử dụng itertools. tích lũyUsing itertools.accumulate
Python
my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 00 import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 466 24 0 import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 48
my_new_list = map(lambda x: x * 5, my_list) 8>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 2255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 7>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 0__5555552553Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6 222
6 240>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 5import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 70my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 9 my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 27my_new_list = map(lambda x: x * 5, my_list) 3 import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 74
6 243>>> (s * 5).tolist() [5, 10, 15, 20, 25] 2 my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 5import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 78my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 9 my_new_list = [] for i in my_list: my_new_list.append(i * 5) >>> print(my_new_list) [5, 10, 15, 20, 25] 27my_new_list = map(lambda x: x * 5, my_list) 3 import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 74
Input : list1 = [3, 2, 4] Output : 24 6import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 84import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 85>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 87
Input : list1 = [3, 2, 4] Output : 24 6import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 89import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 85>>> (s * 5).tolist() [5, 10, 15, 20, 25] 3import pandas as pd s = pd.Series(my_list) >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64 87
Output:
6 24