-1

I want to multiply each element in a list by a given number.

For example, when I key in the following on the command prompt:

C:\Users\bella\projects\testing1> test.py 5,10,15,20,25,30 2

The result should be: [10,20,30,40,50,60] (Note: each element is multiplied by 2)

or

C:\Users\bella\projects\testing1> test.py 5,10,15,20,25,30 0.5

The result should be: [2.5,5,7.5,10,12.5,15] (Note: each element is multiplied by 0.5)

Here is my code...

import sys

user_input = sys.argv[1]
u_split = user_input.split(",")
option = sys.argv[2]

n = map(lambda x:x*(option), user_input)
print(n)

This is what I get on the command prompt:

C:\Users\bella\projects\testing1> test.py 10,20,30,40,50,60 2
<map object at 0x03588160>
Bella Swan
  • 19
  • 4

1 Answers1

0

You need to parse your input into numbers first. And then, map is lazy - you'll need a list() to turn the map into a list.

import sys

user_input = sys.argv[1]
u_split = user_input.split(",")
option = float(sys.argv[2]) # parse into float

n = list(map(lambda x: float(x)*option, u_split))
print(n)

Or simply:

n = [float(x) * option for x in u_split]
rdas
  • 20,604
  • 6
  • 33
  • 46