Python Forum
List conversion and multiplication
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List conversion and multiplication
#1
Hello,

I am trying to work my way through some practice exercises where the objective is to convert a string containing numbers, so that I am able to multiply those numbers to each other. A key requirement is that whenever there is a zero that number gets skipped.

For example, if I have the string “123405”, the multiplication would look like this: 1*2*3*4*5 yielding 120.

My problem isn’t so much the if statement, as much as how to convert the string to an integer or float and then parse this to a series of numbers that I can multiply.

Could you please provide pointers or suggestions on how to approach this problem?

Thank you!
Reply
#2
list('123405') would give you a list of the individual characters in the list. You could then use a for loop, convert each character to an integer with the int() built-in, and multiply it (if it's not zero).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you that answered my question perfectly.

Wishing you all the best for the holidays and new year!
Reply
#4
You don't need to create the list, since strings are also sequences. That means you can iterate over them directly:

>>> for c in "foo":
...     print(c)
... 
f
o
o
>>> 
Reply
#5
Thank you for responding. That makes sense. Also my question has been answered so I will be closing this thread. Btw, happy new year!
Reply
#6
Starting from Python 3.8 there is prod function in built-in math module.

>>> import math
>>> s = '123405'
>>> nums = (int(num) for num in s if num != '0')  
>>> math.prod(nums)
120
In code above nums is generator expression and generators will exhaust after first usage.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiplication Table code alexsendlegames100 3 1,317 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  Try to solve GTG multiplication table problem. Frankduc 6 1,937 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Need help with my Python code (Multiplication) NeedHelpPython 2 1,630 Oct-04-2021, 12:09 PM
Last Post: Pedroski55
  Matrix Multiplication Issue VIJENDRA 1 1,831 Dec-19-2019, 06:16 PM
Last Post: Gribouillis
  Multiplication between a list and a variable doug2019 2 2,122 Oct-08-2019, 04:10 AM
Last Post: doug2019
  Multiplication Table number margins CJ707 4 2,365 Sep-18-2019, 02:16 PM
Last Post: CJ707
  multiplication by successive addition Zebrol 1 3,471 Sep-14-2019, 05:37 PM
Last Post: ichabod801
  Filter to List and/or Tuple conversion vindo 3 2,714 May-18-2019, 05:40 AM
Last Post: perfringo
  float multiplication - unexpected output inesk 3 3,256 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE
  Print every multiplication of recursion Arontbt 4 3,678 Apr-24-2018, 10:01 PM
Last Post: Arontbt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020