如何将列表中的每个元素乘以数字? [重复]

时间:2021-09-16 12:53:55

This question already has an answer here:

这个问题在这里已有答案:

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]

7 个解决方案

#1


49  

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]

#2


9  

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。

#3


6  

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

用于循环

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

#4


6  

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的方法,使用列表理解基本上是最恐怖的方式。

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 pyhton-2.x you don't need that because map return a list by default).

此代码将my_list中的所有项传递给5的__mul__方法,并返回类似迭代器的对象(在python-3.x中)。然后,您可以使用内置函数的list()将迭代器转换为列表(在pyhton-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)

#5


2  

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 complicated way that was probably covered in class

是你可以做到的一种方式......你的老师可能知道一种不那么复杂的方式,可能在课堂上有所涉及

#6


0  

With map (not as good, but another approach to the problem):

使用地图(不是很好,但问题的另一种方法):

def timesfive(number): return number*5
list(map(timesfive,[5, 10, 15, 20, 25]))

#7


0  

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]

#1


49  

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]

#2


9  

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。

#3


6  

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

用于循环

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

#4


6  

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的方法,使用列表理解基本上是最恐怖的方式。

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 pyhton-2.x you don't need that because map return a list by default).

此代码将my_list中的所有项传递给5的__mul__方法,并返回类似迭代器的对象(在python-3.x中)。然后,您可以使用内置函数的list()将迭代器转换为列表(在pyhton-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)

#5


2  

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 complicated way that was probably covered in class

是你可以做到的一种方式......你的老师可能知道一种不那么复杂的方式,可能在课堂上有所涉及

#6


0  

With map (not as good, but another approach to the problem):

使用地图(不是很好,但问题的另一种方法):

def timesfive(number): return number*5
list(map(timesfive,[5, 10, 15, 20, 25]))

#7


0  

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]