简体   繁体   English

Function 将列表/数组的元素乘以 python 中的数字

[英]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.

相关问题 将数组相乘的DRYing函数 - DRYing Function that multiplies array JavaScript函数可产生一个数组,该数组根据前一个整数乘以x倍 - JavaScript function that produces an array that multiplies x number of times based on previous integer 在 Python 中创建一个 function 生成一个 2 列数组,计算列表的每个元素中出现的 substring 和每个元素的长度? - Create a function in Python that generates a 2-column array counting the substring occurence in each element of a list and the length of each element? 接受一个二维数组,乘以一个整数,然后在 C++ 中返回新的二维数组的函数? - Function that accepts a 2D array, multiplies it by an integer, and returns the new 2D array in C++? 用“元素编号”替换数组中的元素(Python) - Replacing Elements in an Array with its “Element Number” (Python) Python:如何将列表或数组分配给数组元素? - Python:How to assign a list or an array to an array element? Python 列表索引以查找索引元素的编号(如果元素相同) - Python List indexing for finding number of the indexed element if the element is the same 如何检查列表元素是否存在于python数组中 - How to check if list element is present in array in python 创建具有不同元素大小的数组或列表python - create an array or list with different element sizes python 限制 Python 中数组/列表中输入值的数量 - Limiting number of input values in an array/list in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM