Python 如何将列表中的每个元素乘以一个数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35166633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I multiply each element in a list by a number?
提问by DJ bigdawg
I have a list:
我有一个清单:
my_list = [1, 2, 3, 4, 5]
How can I multiply each element in my_list
by 5? The output should be:
如何将每个元素乘以my_list
5?输出应该是:
[5, 10, 15, 20, 25]
采纳答案by Alexander
You can just use a list comprehension:
您可以只使用列表理解:
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]
Note that a list comprehension is generally a more efficient way to do a for
loop:
请注意,列表推导式通常是执行for
循环的更有效方法:
my_new_list = []
for i in my_list:
my_new_list.append(i * 5)
>>> print(my_new_list)
[5, 10, 15, 20, 25]
As an alternative, here is a solution using the popular Pandas package:
作为替代方案,这里有一个使用流行的 Pandas 包的解决方案:
import pandas as pd
s = pd.Series(my_list)
>>> s * 5
0 5
1 10
2 15
3 20
4 25
dtype: int64
Or, if you just want the list:
或者,如果您只想要列表:
>>> (s * 5).tolist()
[5, 10, 15, 20, 25]
回答by Joran Beasley
from functools import partial as p
from operator import mul
map(p(mul,5),my_list)
is one way you could do it ... your teacher probably knows a much less complicatedway that was probably covered in class
是你可以做到的一种方法......你的老师可能知道一种可能在课堂上讲过的简单得多的方法
回答by wasp8898
Since I think you are new with Python, lets do the long way, iterate thru your list using for loop and multiply and append each element to a new list.
因为我认为您是 Python 新手,所以让我们走得更远,使用 for 循环遍历您的列表,然后将每个元素相乘并将其附加到新列表中。
using for loop
使用 for 循环
lst = [5, 20 ,15]
product = []
for i in lst:
product.append(i*5)
print product
using list comprehension, this is also same as using for-loop but more 'pythonic'
使用列表理解,这也与使用 for-loop 相同,但更“pythonic”
lst = [5, 20 ,15]
prod = [i * 5 for i in lst]
print prod
回答by David Hoelzer
You can do it in-place like so:
你可以像这样就地做:
l = [1, 2, 3, 4, 5]
l[:] = [x * 5 for x in l]
This requires no additional imports and is very pythonic.
这不需要额外的导入并且非常pythonic。
回答by Kasramvd
A blazingly faster approach is to do the multiplication in a vectorized manner instead of looping over the list. Numpy has already provided a very simply and handy way for this that you can use.
一种极快的方法是以向量化的方式进行乘法运算,而不是遍历列表。Numpy 已经为此提供了一种非常简单且方便的方法供您使用。
>>> import numpy as np
>>>
>>> my_list = np.array([1, 2, 3, 4, 5])
>>>
>>> my_list * 5
array([ 5, 10, 15, 20, 25])
Note that this doesn't work with Python's native lists. If you multiply a number with a list it will repeat the items of the as the size of that number.
请注意,这不适用于 Python 的本机列表。如果您将一个数字与一个列表相乘,它将重复该数字的项目作为该数字的大小。
In [15]: my_list *= 1000
In [16]: len(my_list)
Out[16]: 5000
If you want a pure Python-based approach using a list comprehension is basically the most Pythonic way to go.
如果您想要使用列表推导式的纯基于 Python 的方法,则基本上是最 Pythonic 的方法。
In [6]: my_list = [1, 2, 3, 4, 5]
In [7]: [5 * i for i in my_list]
Out[7]: [5, 10, 15, 20, 25]
Beside list comprehension, as a pure functional approach, you can also use built-in map()
function as following:
除了列表理解,作为一种纯函数方法,您还可以使用内置map()
函数,如下所示:
In [10]: list(map((5).__mul__, my_list))
Out[10]: [5, 10, 15, 20, 25]
This code passes all the items within the my_list
to 5
's __mul__
method and returns an iterator-like object (in python-3.x). You can then convert the iterator to list using list()
built in function (in Python-2.x you don't need that because map
return a list by default).
此代码通过了所有内的物品my_list
到5
的__mul__
方法并返回迭代状物体(在python-3.X)。然后,您可以使用list()
内置函数将迭代器转换为列表(在 Python-2.x 中您不需要它,因为map
默认情况下返回列表)。
benchmarks:
基准:
In [18]: %timeit [5 * i for i in my_list]
463 ns ± 10.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [19]: %timeit list(map((5).__mul__, my_list))
784 ns ± 10.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [20]: %timeit [5 * i for i in my_list * 100000]
20.8 ms ± 115 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [21]: %timeit list(map((5).__mul__, my_list * 100000))
30.6 ms ± 169 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [24]: arr = np.array(my_list * 100000)
In [25]: %timeit arr * 5
899 μs ± 4.98 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
回答by whackamadoodle3000
With map (not as good, but another approach to the problem):
使用地图(不太好,但问题的另一种方法):
list(map(lambda x: x*5,[5, 10, 15, 20, 25]))
回答by Col. John Hannibal Smith
Best way is to use list comprehension:
最好的方法是使用列表理解:
def map_to_list(my_list, n):
# multiply every value in my_list by n
# Use list comprehension!
my_new_list = [i * n for i in my_list]
return my_new_list
# To test:
print(map_to_list([1,2,3], -1))
Returns: [-1, -2, -3]
返回: [-1, -2, -3]
回答by Vityata
Multiplying each element in my_list
by k
:
乘以每个元件在my_list
由k
:
k = 5
my_list = [1,2,3,4]
result = list(map(lambda x: x * k, my_list))
resulting in: [5, 10, 15, 20]
导致: [5, 10, 15, 20]