简体   繁体   English

如何在python中使用re.findall

[英]How to use re.findall in python

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 . 我还想命名第一个结果a ,第二个b ,第三个c And then calculate a+b or a+c , something like that. 然后计算a+ba+c ,就像这样。 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? 如果re.findall在这里无法解决我的问题,那么还有其他方法可以实现这一目标吗?

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. 如果您希望这些数字使用字符串表示(我不明白为什么,如果是数字,则让它成为数字),然后删除int()强制转换。

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. 您可能要在这里重新考虑逻辑。

If you want, you can use tuple assignment on the LHS: 如果需要,可以在LHS上使用元组分配

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

This is equivalent to writing (a,b,c) = ... ; 这相当于写(a,b,c) = ... ; you no longer need to put parentheses around a tuple if it's on LHS of an assignment. 如果是在作业的LHS上,则不再需要在元组周围加上括号。

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. 就像@SterlingArcher所说的那样,这是不好的编码风格,因为除非得到正好三个项目,或者如果字符串不正确或正则表达式失败,否则将产生错误。

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. 一种策略是使用尾随的_作为无关紧要的变量来吸收任何其他项: a,b,c,_ = ...但是,如果您的字符串或正则表达式没有给出至少三个,这仍然会失败项目。

This solution may not help the particular problem with re.findall, but this solution will allow you to do the calculation you desire. 该解决方案可能无法解决re.findall的特定问题,但是该解决方案将使您能够进行所需的计算。 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. 这段代码可能效率不高,但易于理解,可以完成您需要完成的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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