1

Here is my program:

import re
string = "I have 56 apples, Tom has 34 apples, Mary has 222 apples"

apple = re.findall(r'\d{1,100}', string)
print (apple)

And the outcome is:

['56', '34', '222']

I want to name the three numbers above so that I can do some calculation. I also want to name the first outcome a, second one b, third one c. And then calculate a+b or a+c, something like that. Could anyone tell me how to do it.

If re.findall can't solve my case here, is there another way to achieve this goal?

5
  • 1
    what is wrong with apple[0] + apple[1]? Commented Nov 22, 2016 at 5:18
  • 1
    What if you get 2 numbers, or 7? Commented Nov 22, 2016 at 5:18
  • 1
    ...or none at all. Commented Nov 22, 2016 at 5:21
  • 1
    Tip: never call a string string, that will shadow the builtin library (import string). Call your string s, or whatever. Commented Nov 22, 2016 at 18:43
  • Instead of doing '\d{1,100}', you can just do '\d+' to do any number of digits (at least one). I'm assuming that you don't have a hard limit of 100 digits, and you just want to allow for large numbers :) . Commented Nov 22, 2016 at 18:58

3 Answers 3

2

If you want, you can use tuple assignment on the LHS:

a,b,c = re.findall(r'\d{1,100}', string)

This is equivalent to writing (a,b,c) = ... ; you no longer need to put parentheses around a tuple if it's on LHS of an assignment.

This is bad coding style as @SterlingArcher said, because unless you get exactly three items, or if your string was bad, or regex failed, you get an error.

One tactic is to use a trailing _ as a don't-care variable to soak up any extra items: a,b,c,_ = ... But this will still break if your string or regex did not give at least three items.

Sign up to request clarification or add additional context in comments.

5 Comments

You don't need parentheses at all, whether it's on the LHS or the RHS. Unless of course you have to disambiguate the syntax, ie. in a function call.
You used to need parentheses, in very old 2.x Python. Or to disambiguate e.g. (a,b,c) + (d,e) is not a,b,c + d,e
I was going to ask about older versions of Python, because I wasn't sure. Thanks for clearing it up. And yes, you still need them sometimes for disambiguation (as we both have said).
I know when parentheses are needed, I just wasn't aware of the differences between 'modern' Python and the older versions of Python 2.x in that regard.
2

There's an easier way, with a simple loop check.

string = "I have 56 apples, Tom has 34 apples, Mary has 222 apples"
x = [int(num) for num in string.split() if num.isdigit()]

>>> x
[56, 34, 222]

This will split the string and check if it's a number, and return it to a list. If you desire these numbers to be in string for (I can't see why, if it's a number, let it be a number) then remove the int() casting.

The issue with your question, is that if you want to assign these parsed numbers to variables, you'll have to do it manually, which is bad. If you end up having a dynamic number of numbers returned, this will be incredibly tedious and potentially unreliable. You may want to reconsider the logic here.

Comments

0

This solution may not help the particular problem with re.findall, but this solution will allow you to do the calculation you desire. Use a dictionary:

>>>applesDict = {"My apples": 56, "Tom apples": 34, "Mary apples":222}

now you can store and solve for any calculation.

>>>c = applesDict["My apples"]
>>>d = applesDict["Tom apples"]

print c + d
>>> 90

This code may not be as efficient but easy to understand and gets what you need done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.