我在Python循环中遇到正则表达式检测问题,为什么?

huangapple go评论88阅读模式
英文:

Why i'm facing Regex detection problem inside a loop in Python

问题

我在使用正则表达式时遇到了非常奇怪的问题。

```python
weight = 'abcd'
description = 'ml'
for symbol in Syntax['symbol']:
    print(symbol)
    weight = re.findall(symbol, description)
print(weight)
输出 --> []

Syntax是一个数据框,其中包含不同的单位,还包含在symbol列中的***"ml",我已经手动打印了symbol变量,它打印出所需的单位"ml"***,它将在循环中设置为模式,但仍然返回[]或None,使用re.match()时。但是,当我尝试下面的代码时

description = 'ml'
pattern = 'ml'
print(re.findall(pattern, description))

它打印出***"ml"***,为什么?上面和上面的代码在逻辑上是相同的。

[![在此输入图片描述][1]][1]


<details>
<summary>英文:</summary>

I&#39;m facing very weird problem while using regex.

weight='abcd'
description='ml'
for symbol in Syntax['symbol']:
print(symbol)
weight=re.findall(symbol,description)
print(weight)
output --> []

***Syntax is a data frame*** that contains different units, also contains ***&quot; ml &quot;*** inside symbol column, i have manually printed the symbol variable it prints required unit that is ***&quot;ml&quot;*** which will be set as pattern in loop but still it returns [] OR None while using re.match().
But when i try code below

description='ml'
pattern='ml'
print(re.findall(pattern,description)

it prints ***&quot;ml&quot;***, Why ??? Both above and Top code are logically same.

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/yN84A.png

</details>


# 答案1
**得分**: 1

在顶部的代码中,由于`print(weight)`位于循环外部,所以您只打印了最终正则表达式搜索的结果。如果“ml”在您的数据框中的某个地方,那就很好,但如果`symbol`的*最后一个*值与`description`中的任何内容都不匹配,那么正则表达式将找不到任何匹配项,您将不会得到任何输出。

尝试在`for`循环内部打印`weight`,看看您会得到什么输出。

<details>
<summary>英文:</summary>

In the top code, you&#39;re only printing the result of the final regex search, since `print(weight)` is outside your loop. It&#39;s all well and good if &quot;ml&quot; is somewhere in your data frame, but if the *last* value of `symbol` doesn&#39;t match anything in `description`, the regex won&#39;t find any matches and you won&#39;t get any output. 

Try printing `weight` inside the `for` loop and see what output you get.

</details>



# 答案2
**得分**: 0

    description='ml'
    weight=0
    for symbol in self.units['symbol']:
        print("units in Syntax dataframe : ", symbol)
        weight=re.findall(symbol, description)
        if weight!=[]:
            break
    print(weight)

<details>
<summary>英文:</summary>

    description=&#39;ml&#39;
    weight=0
    for symbol in self.units[&#39;symbol&#39;]:
        print(&quot;units in Syntax dataframe : &quot;,symbol)
        weight=re.findall(symbol,description)
        if weight!=[]:
            break
    print(weight)
I have understood the problem, i was not stopping the loop when the &#39;ml&#39; is found, that&#39;s why it was printing [] or None

</details>



huangapple
  • 本文由 发表于 2020年1月4日 00:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定