小编Puf*_*GDI的帖子

为什么re2的结果与Python中的re模块不同?

我尝试使用re2.

import re
print re.search('cde', 'abcdefg').group(0)
Run Code Online (Sandbox Code Playgroud)

结果:

cde
Run Code Online (Sandbox Code Playgroud)

但是re2的结果是不同的

import re2
print re2.search('cde', 'abcdefg').group(0)
Run Code Online (Sandbox Code Playgroud)

结果:

1
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
Run Code Online (Sandbox Code Playgroud)
  1. 为什么每次执行新的字符串模式时re2输出数字1
  2. 如何隐藏数字1
  3. 为什么结果不同re module(not found => return None)?

re2版本是0.2.20.Python是2.7

谢谢

python regex re2

6
推荐指数
1
解决办法
1394
查看次数

如何将集合列表作为单独的参数传递给函数?

我已经创建了一个我想要传递给set.intersection()的集合列表

例如:

List_of_Sets = [{1,2,3},{3,4,5},{5,6,7}]
set.intersection(List_of_Sets)
Run Code Online (Sandbox Code Playgroud)

结果:

TypeError: descriptor 'intersection' requires a 'set' object but received a 'list'
Run Code Online (Sandbox Code Playgroud)

期望的输出:

{3,5}
Run Code Online (Sandbox Code Playgroud)

如何将列表中的每个集合作为单独的参数传递到set.intersection()?

python function list set

3
推荐指数
1
解决办法
1962
查看次数

如何在Python中的int之前添加加号?

我需要以非常特定的格式生成输出,并且正整数必须在它们前面有一个加号.我正在使用numpy数组,并尝试这样的东西:

    if(int(P[pnt])>0):
        P[pnt] += np.insert(P[pnt-1],0,"+")
Run Code Online (Sandbox Code Playgroud)

但它永远不会添加加号作为数字的一部分,而是作为一个不同的实例..

我也尝试将其保存在不同的文件中,然后从那里修改它(使用re.sub()等...)但没有运气:(

我的输出看起来像这样:

(+1 2 -4 +5 -3)
(+1 2 3 -5 4)
(+1 2 3 -4 5)
(+1 2 3 4 5)
Run Code Online (Sandbox Code Playgroud)

并且应该如下所示:

(+1 +2 -4 +5 -3)
(+1 +2 +3 -5 +4)
(+1 +2 +3 -4 +5)
(+1 +2 +3 +4 +5)
Run Code Online (Sandbox Code Playgroud)

如有必要,我可以分享整个代码......

谢谢!!:)

python arrays numpy

0
推荐指数
1
解决办法
1520
查看次数

标签 统计

python ×3

arrays ×1

function ×1

list ×1

numpy ×1

re2 ×1

regex ×1

set ×1