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
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 : 24
Phươ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]
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
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 mul[] function of operator module.
6 240
6 241
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=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
my_new_list = []
for i in my_list:
my_new_list.append[i * 5]
>>> print[my_new_list]
[5, 10, 15, 20, 25]
97Dướ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 240
6 241
>>> [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]
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 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 240
import pandas as pd
s = pd.Series[my_list]
>>> s * 5
0 5
1 10
2 15
3 20
4 25
dtype: int64
48my_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 7: Sử dụng itertools. tích lũy
Python
Input : 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