1

I've come across this problem which I have to solve by creating a function that multiplies all float numbers by a predetermined float (parameter) but the problem is I cant get it to recognize floats, only ints (recognize 2.5 as 2 and 5)

For example, the code I've seen here with 2 as a parameter would run "2.5,3.5,4.5" and result in "4.10,6.10,8.10" but I would like to end up with "5,7,9". Any ideas?

Here is the code that solves it but only for integers:

def myfunction(mystring, by):
    return re.sub(
        re.compile("\d+"), 
        lambda matchobj: str(int(matchobj.group(0))*by), 
        mystring
        )
  • 1
    When you say "all floats in a line" I think you mean "in a string". Example: "2.4 6.7 1.8" is a string. So you really first want to convert that into separate strings: `"2.4 6.7 1.8".split()`, then convert them into a list of floats. You can do all that in a list comprehension: `[float(s) for s in "2.4 6.7 1.8".split()]` – smci Jun 23 '20 at 06:24
  • Ah, your issue is you don't know how to change the regex `'\d+'` to handle floats... please see the doc. Hint: `\.` matches a decimal point. Remember that in a float, the decimal part is optional, e.g. `7 7. 7.5` can all be floats. – smci Jun 23 '20 at 06:27
  • *"would run "2.5,3.5,4.5" and result in "4.10,6.10,8.10"* is wrong, you're only multiplying each individually digit by `by`. Even in the simple case where `by` is only an integer (not itself a float), this will give the wrong answer if there's any carries, i.e. it'll always be wrong unless `by` is an integer from 0..4, not 5 or more. – smci Jun 23 '20 at 06:31
  • Related: [How to extract numbers from a string in Python?](https://stackoverflow.com/questions/4289331/how-to-extract-numbers-from-a-string-in-python), [Regex for positive float numbers](https://stackoverflow.com/questions/6029674/regex-for-positive-float-numbers), etc. – smci Jun 23 '20 at 06:33
  • Welcome to SO. [You can accept one answer (if it helps you) by click on the big gray check button on its left side. If you wish you can also upvote answers by clicking on the upper gray triangle.](https://meta.stackoverflow.com/questions/354584/teaching-new-users-how-to-accept-an-answer) If you want to post new questions involving needing a regex for more complicated input, then post a new separate question, don't edit this one. – smci Dec 15 '20 at 06:14

3 Answers3

0

When you say "all floats in a line" you mean "in a string". So you really first want to convert that into separate strings: "2.4 6.7 1.8".split(), then convert them into a list of floats. Also you don't need regexes for this.

You can do all that in a list comprehension:

>>> numbers = [float(s) for s in "2.4 6.7 1.8".split()]

[2.4, 6.7, 1.8]

or even if you wanted to you could include the multiplication by by:

[by * float(s) for s in "2.4 6.7 1.8".split()]

def myfunction(mystring, by):
    return [by * float(s) for s in "2.4 6.7 1.8".split()]

Now if you insist on using regex (you don't need it and you shouldn't), your issue is you don't know how to change the regex r'\d+' regex to handle floats... please see the doc. Hint: . matches a decimal point. Remember that in a float, the decimal part is optional, e.g. 7 7. 7.5 can all be floats.

So, hint: what's a regex with one or more digits, optionally followed by decimal point and zero or more digits?

smci
  • 32,567
  • 20
  • 113
  • 146
  • thanks! now would this also work if the string had chars in it? for example "there are 2.5cm in 1 inch" would become "there are 5cm in 2 inch"? – Bruno Tannús Jun 24 '20 at 07:03
  • BrunoTannús: sure, but that's a new separate followup question :) Try writing the regex yourself, and post a question showing your own work if you get stuck. Remember `(abc)?` matches an optional capture group, and `(abc|def|gh)?` matches multiple alternative expressions. Also, always surround your regexes in Python with `r'...'` rawstring to protect magic characters like backslash `\\`. But first plaese skim all the many good regex tutorials, how-tos, Q&As on SO, blogs... – smci Jun 24 '20 at 17:53
0

You can simply do it in a single line by using the split() method of string and the map() function.

def mymethod(mystring, by):
     return " ".join(map(lambda num: float(num) * by, mystring.split()))

Or more tersely:

mymethod = lambda mystring, by: " ".join(map(lambda num: float(num) * by, mystring.split()))

To match a float using re:

r"([\+-])?\d*\.?\d+"
Amal K
  • 4,359
  • 2
  • 22
  • 44
0

If you are okay with using a whole new library for a small task, then below is your solution using re

example = "2.5,3.5,4.5"
example = re.sub("\"", "", example)
example = re.split(",", example)
example = [2*float(e) for e in example]

But you'd be better off using split like in the answer by @smci

Piyush Kansal
  • 103
  • 1
  • 9