<i id='RHsmW'><tr id='RHsmW'><dt id='RHsmW'><q id='RHsmW'><span id='RHsmW'><b id='RHsmW'><form id='RHsmW'><ins id='RHsmW'></ins><ul id='RHsmW'></ul><sub id='RHsmW'></sub></form><legend id='RHsmW'></legend><bdo id='RHsmW'><pre id='RHsmW'><center id='RHsmW'></center></pre></bdo></b><th id='RHsmW'></th></span></q></dt></tr></i><div id='RHsmW'><tfoot id='RHsmW'></tfoot><dl id='RHsmW'><fieldset id='RHsmW'></fieldset></dl></div>

    <bdo id='RHsmW'></bdo><ul id='RHsmW'></ul>
  • <tfoot id='RHsmW'></tfoot>
  • <legend id='RHsmW'><style id='RHsmW'><dir id='RHsmW'><q id='RHsmW'></q></dir></style></legend>

      <small id='RHsmW'></small><noframes id='RHsmW'>

      1. re.findall 行为怪异

        re.findall behaves weird(re.findall 行为怪异)
          <legend id='bNl9D'><style id='bNl9D'><dir id='bNl9D'><q id='bNl9D'></q></dir></style></legend>

            <tbody id='bNl9D'></tbody>
          <tfoot id='bNl9D'></tfoot>

          • <small id='bNl9D'></small><noframes id='bNl9D'>

                <bdo id='bNl9D'></bdo><ul id='bNl9D'></ul>

                <i id='bNl9D'><tr id='bNl9D'><dt id='bNl9D'><q id='bNl9D'><span id='bNl9D'><b id='bNl9D'><form id='bNl9D'><ins id='bNl9D'></ins><ul id='bNl9D'></ul><sub id='bNl9D'></sub></form><legend id='bNl9D'></legend><bdo id='bNl9D'><pre id='bNl9D'><center id='bNl9D'></center></pre></bdo></b><th id='bNl9D'></th></span></q></dt></tr></i><div id='bNl9D'><tfoot id='bNl9D'></tfoot><dl id='bNl9D'><fieldset id='bNl9D'></fieldset></dl></div>
                • 本文介绍了re.findall 行为怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  源字符串是:

                  # Python 3.4.3
                  s = r'abc123d, hello 3.1415926, this is my book'
                  

                  这是我的模式:

                  pattern = r'-?[0-9]+(\.[0-9]*)?|-?\.[0-9]+'
                  

                  但是,re.search 可以给我正确的结果:

                  however, re.search can give me correct result:

                  m = re.search(pattern, s)
                  print(m)  # output: <_sre.SRE_Match object; span=(3, 6), match='123'>
                  

                  re.findall 只是转储一个空列表:

                  re.findall just dump out an empty list:

                  L = re.findall(pattern, s)
                  print(L)  # output: ['', '', '']
                  

                  为什么不能 re.findall 给我预期的列表:

                  why can't re.findall give me the expected list:

                  ['123', '3.1415926']
                  

                  推荐答案

                  s = r'abc123d, hello 3.1415926, this is my book'
                  print re.findall(r'-?[0-9]+(?:.[0-9]*)?|-?.[0-9]+',s)
                  

                  您在使用原始模式时不需要转义两次.

                  You dont need to escape twice when you are using raw mode.

                  输出:['123', '3.1415926']

                  返回类型也是字符串的列表.如果您希望返回类型为 integersfloats,请使用 map

                  Also the return type will be a list of strings. If you want return type as integers and floats use map

                  import re,ast
                  s = r'abc123d, hello 3.1415926, this is my book'
                  print map(ast.literal_eval,re.findall(r'-?[0-9]+(?:.[0-9]*)?|-?.[0-9]+',s))
                  

                  输出:[123, 3.1415926]

                  这篇关于re.findall 行为怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  <i id='FZHi1'><tr id='FZHi1'><dt id='FZHi1'><q id='FZHi1'><span id='FZHi1'><b id='FZHi1'><form id='FZHi1'><ins id='FZHi1'></ins><ul id='FZHi1'></ul><sub id='FZHi1'></sub></form><legend id='FZHi1'></legend><bdo id='FZHi1'><pre id='FZHi1'><center id='FZHi1'></center></pre></bdo></b><th id='FZHi1'></th></span></q></dt></tr></i><div id='FZHi1'><tfoot id='FZHi1'></tfoot><dl id='FZHi1'><fieldset id='FZHi1'></fieldset></dl></div>
                • <legend id='FZHi1'><style id='FZHi1'><dir id='FZHi1'><q id='FZHi1'></q></dir></style></legend>

                  <tfoot id='FZHi1'></tfoot>

                    <bdo id='FZHi1'></bdo><ul id='FZHi1'></ul>

                        <tbody id='FZHi1'></tbody>
                      • <small id='FZHi1'></small><noframes id='FZHi1'>