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: int64
Hoặ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
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ứcmy_new_list = [] for i in my_list: my_new_list.append[i * 5] >>> print[my_new_list] [5, 10, 15, 20, 25]
5; trongmy_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ụngmy_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ăngmy_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.my_new_list = [] for i in my_list: my_new_list.append[i * 5] >>> print[my_new_list] [5, 10, 15, 20, 25]
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
1 vớimy_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ộtmy_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 chomy_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ầnmy_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,my_new_list = [] for i in my_list: my_new_list.append[i * 5] >>> print[my_new_list] [5, 10, 15, 20, 25]
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ọcimport 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.import pandas as pd s = pd.Series[my_list] >>> s * 5 0 5 1 10 2 15 3 20 4 25 dtype: int64
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]
64Examples:
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6
Input : list1 = [3, 2, 4] Output : 24
6
24
3
2 >>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
67Traversalmy_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
Phương pháp 5: Sử dụng hàm mul [] của mô -đun toán tử. & Nbsp;
Đầu tiên chúng ta phải nhập mô -đun toán tử sau đó sử dụng hàm mul [] của mô -đun toán tử nhân tất cả các giá trị trong danh sách. & NBSP;
6
24
3>>> [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]
03my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
25my_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 my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
37
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
my_new_list = map[lambda x: x * 5, my_list]
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
Input : list1 = [3, 2, 4] Output : 246
6 249
Bắt đầu Python 3.8, một hàm prod đã được đưa vào mô -đun toán học trong thư viện tiêu chuẩn, do đó không cần phải cài đặt các thư viện bên ngoài.
6 240
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
436 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]
64>>> [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]
7my_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=60
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=63
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=67
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=60__5555552553
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6222
Input : list1 = [3, 2, 4] Output : 246
Input : list1 = [3, 2, 4] Output : 247
Input : list1 = [3, 2, 4] Output : 246
Input : list1 = [3, 2, 4] Output : 249
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 240
6 241
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=60
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=63
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=67
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=60__5555552553
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6222
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 : 246
6 247
Input : list1 = [3, 2, 4] Output : 246
6 249
Output:
6 24
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 240
6 241“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 240
6 241
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=60
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=63
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=67
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=60__5555552553
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6222
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 : 246
6 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 240
6 241
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=60
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=63
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=67
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=60__5555552553
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6222
6 240
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 6 242
6 243
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 6 245
Input : list1 = [3, 2, 4] Output : 246
6 247
Input : list1 = [3, 2, 4] Output : 246
6 249
Output:
6 24
Phương pháp 3 sử dụng chức năng Lambda: Sử dụng Numpy.ArrayUsing mul[] function of operator module.
Định nghĩa của Lambda không bao gồm một câu lệnh trả về của người Viking, nó luôn chứa một biểu thức được trả về. Chúng tôi cũng có thể đặt một định nghĩa Lambda bất cứ nơi nào một hàm được mong đợi và chúng tôi không phải gán nó cho một biến. Đây là sự đơn giản của các chức năng Lambda. Hàm giảm [] trong Python có chức năng và một danh sách như một đối số. Hàm được gọi với chức năng Lambda và danh sách và kết quả giảm mới được trả về. Điều này thực hiện một hoạt động lặp đi lặp lại trên các cặp của danh sách.
Python3
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
00 my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
016 240
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
03my_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=60
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=63
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62255555555
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]
03my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
25my_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 my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
296 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]
03my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
25my_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 my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
37Input : list1 = [3, 2, 4] Output : 246
6 249
Input : list1 = [3, 2, 4] Output : 246
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
97Phương pháp 4 Sử dụng chức năng Prod của Thư viện toán học: Sử dụng Math.Prod Using traversal by index
Python3
Bắt đầu Python 3.8, một hàm prod đã được đưa vào mô -đun toán học trong thư viện tiêu chuẩn, do đó không cần phải cài đặt các thư viện bên ngoài.
6 240
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
436 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]
646 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]
67>>> [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]
7my_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=60
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=63
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=67
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=60__5555552553
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6222
Input : list1 = [3, 2, 4] Output : 246
Input : list1 = [3, 2, 4] Output : 247
Input : list1 = [3, 2, 4] Output : 246
Input : list1 = [3, 2, 4] Output : 249
Phương pháp 5: Sử dụng hàm mul [] của mô -đun toán tử. & Nbsp;Using itertools.accumulate
Đầu tiên chúng ta phải nhập mô -đun toán tử sau đó sử dụng hàm mul [] của mô -đun toán tử nhân tất cả các giá trị trong danh sách. & NBSP;
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
00 my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
736 240
my_new_list = map[lambda x: x * 5, my_list]
3my_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=60
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
3Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=63
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=62255555555
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=67
>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=60__5555552553
Input : list1 = [1, 2, 3] Output : 6 Explanation: 1*2*3=6222
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
85>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
2 >>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
3my_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]
89>>> [s * 5].tolist[]
[5, 10, 15, 20, 25]
7 my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
91Input : list1 = [3, 2, 4] Output : 246
import 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
87Input : list1 = [3, 2, 4] Output : 246
import 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
87Output:
6 24