简体   繁体   中英

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.

Def Multiply ( ListOrArray )... #Return another list with the same quantity of elements but multiplied by 5.

Functional programming in Python makes it quite easy to implement, as you can see in the following snippet:

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:

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM