[英]Function that multiplies element of a list/array by a number in python
I want to create a function that receives an array or a list, and then multiply each element by a Integer number, for example 5.我想创建一个接收数组或列表的 function,然后将每个元素乘以 Integer 数字,例如 5。
Def Multiply ( ListOrArray )... #Return another list with the same quantity of elements but multiplied by 5. Def Multiply ( ListOrArray )... #返回另一个元素数量相同但乘以 5 的列表。
Functional programming in Python makes it quite easy to implement, as you can see in the following snippet: Python 中的函数式编程使其非常容易实现,您可以在以下代码段中看到:
my_numbers = [1,2,3,4,5]
results = list(map(lambda x: x*5, my_numbers))
print(results)
More information about map, lambda and related functions:有关 map、lambda 及相关功能的更多信息:
https://www.learnpython.org/en/Map,_Filter,_Reduce https://www.learnpython.org/en/Map,_Filter,_Reduce
Probably you could try the following也许你可以尝试以下
list = [1,2,3]
def multiplier (a):
b =[]
for i in list:
i = i*5
b.append(i)
return b
a=list
print(multiplier(a))
or或者
def multiplier(*args):
b=[]
for a in args:
a = a*5
b.append(a)
return b
print(multiplier(5,6,7))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.