- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在列表中找到与正则表达式匹配的所有元素。为了减少正则表达式匹配的次数,我通过连接以空格分隔的元素创建了一个字符串,如下所示:
list_a = ["4123", "7648", "afjsdn", "ujaf", "huh23"]
regex_num = r"\d+"
string_a = " ".join(list_a)
num_matches = re.findall(regex_num, string_a)
名单和比赛如下:
list_a: ['4123', '7648', 'afjsdn', 'ujaf', 'huh23']
matches: ['4123', '7648', '23']
现在我有了所有匹配项,我想知道匹配项是元素/标记的一部分还是整个标记。我可以做到这一点的一种方法是将匹配项与实际标记/元素进行比较:
"23" == "huh23"
False
但为此,我需要 token 序列号。哪个不能直接使用。正则表达式匹配唯一可以提供的位置信息是字符级别的匹配范围。
我可以采用的另一条路径是通过循环遍历列表并比较字符串与匹配项(如果存在匹配项)来对所有元素应用正则表达式匹配。
我想尽可能降低此操作的时间复杂度。
是否有更 pythonic 的方法来确定匹配项是否只是 token 的一部分,或者是否有更 pythonic 的方法来查找匹配单词的序列号,以便可以利用初始列表进行字符串比较?
如有任何帮助,我们将不胜感激。提前致谢!
编辑 1:
如果我的列表是这样的:
list_a = ["4123", "7648", "afjsdn", "ujaf", "huh23", "n23kl3l24"]
就像@Artyom Vancyan 在评论中建议的那样
我想要的输出是:
matches_with_slno = [[0,'4123'], [1,'7648'], [4, '23'], [5, '23'], [5,'3'], [5, '24']
最佳答案
使用 itertools
的函数式编程方法:
from itertools import chain, zip_longest
list_a = ["4123", "7648", "afjsdn", "ujaf", "huh23", "n23kl3l24"]
regex = r'\d+'
# pre-compilation of the regex
pattern = re.compile(regex)
# group the matches
grps = ((i, m) for i, m in enumerate(chain(map(pattern.findall, list_a))) if m)
# coupling the groups + flatting
out = list(chain.from_iterable(tuple(zip_longest((i,), matches, fillvalue=i)) for i, matches in grps))
print(out)
#[(0, '4123'), (1, '7648'), (4, '23'), (5, '23'), (5, '3'), (5, '24')]
性能测试
我试图做一个公平的测试:每个答案都有两个实现:
pattern.findall
re.findall
测试的参数是重复次数(以获得统计上稳定的结果)和扩展数据列表大小的长度因子。
在所有情况下,预编译都会使代码的性能更高。列表理解似乎是更好的解决方案,然后是生成器,最后是 itertool。
1 位有效数字,对于小数据,它们的工作原理都是一样的。
使用 timeit
完成的时间测量.
from timeit import timeit
from itertools import chain, zip_longest
import re
def with_itertools_v1(lst):
grps = ((i, m) for i, m in enumerate(chain(map(lambda x: re.findall(r'\d+', x), lst))) if m)
return list(chain.from_iterable(list(zip_longest((i,), matches, fillvalue=i)) for i, matches in grps))
def with_itertools_v2(lst):
pattern = re.compile(r'\d+')
grps = ((i, m) for i, m in enumerate(chain(map(pattern.findall, lst))) if m)
return list(chain.from_iterable(list(zip_longest((i,), matches, fillvalue=i)) for i, matches in grps))
def with_generators_v1(lst):
def gen():
for index, item in enumerate(lst):
yield from [[index, match] for match in re.findall(r"\d+", item)]
return list(gen())
def with_generators_v2(lst):
def gen():
pattern = re.compile(r'\d+')
for index, item in enumerate(lst):
yield from [[index, match] for match in pattern.findall(item)]
return list(gen())
def with_list_comprehension_v1(lst):
return [[index, match] for index, item in enumerate(lst) for match in re.findall(r"\d+", item)]
def with_list_comprehension_v2(lst):
pattern = re.compile(r'\d+')
return [[index, match] for index, item in enumerate(lst) for match in pattern.findall(item)]
funcs = (with_itertools_v1, with_itertools_v2, with_generators_v1, with_generators_v2, with_list_comprehension_v1, with_list_comprehension_v2)
def tester(repetition=5, length_factor=1):
data = ["4123", "7648", "afjsdn", "ujaf", "huh23", "n23kl3l24"] * length_factor
print(f'Test with repeated {repetition}-times with data with {len(data)*length_factor} terms')
for func in funcs:
t = timeit(lambda: func(data), number=repetition)
print(func.__name__, f'ms: {t / repetition * 1e3:3f}')
print()
tester(5, 1)
tester(5, 10)
tester(5, 100)
tester(5, 1000)
结果
Test with repeated 5-times with data with 6 terms
with_itertools_v1 ms: 0.060872
with_itertools_v2 ms: 0.019029
with_generators_v1 ms: 0.023321
with_generators_v2 ms: 0.015797
with_list_comprehension_v1 ms: 0.017898
with_list_comprehension_v2 ms: 0.010917
Test with repeated 5-times with data with 600 terms
with_itertools_v1 ms: 0.248608
with_itertools_v2 ms: 0.142064
with_generators_v1 ms: 0.201945
with_generators_v2 ms: 0.116305
with_list_comprehension_v1 ms: 0.160765
with_list_comprehension_v2 ms: 0.076942
Test with repeated 5-times with data with 60000 terms
with_itertools_v1 ms: 2.165053
with_itertools_v2 ms: 1.612787
with_generators_v1 ms: 1.484747
with_generators_v2 ms: 0.639824
with_list_comprehension_v1 ms: 11.219491
with_list_comprehension_v2 ms: 0.809310
Test with repeated 5-times with data with 6000000 terms
with_itertools_v1 ms: 46.531748
with_itertools_v2 ms: 35.922768
with_generators_v1 ms: 77.271880
with_generators_v2 ms: 20.042708
with_list_comprehension_v1 ms: 22.927475
with_list_comprehension_v2 ms: 16.760901
关于python - 在python中进行正则表达式匹配后获取 token 序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72656456/
我一直在阅读此处的一些帖子和网络上的文章,但我无法为我的应用程序描绘一个基于串行 key 的系统。 http://www.brandonstaggs.com/2007/07/26/implementi
我使用 Win32_BaseBoard SerialNumber 属性来获取主板序列号并且它在大部分时间都可以工作,但有时在某些计算机中我会得到 SerialNumber = "Base Board
我有以下数据: ID;NAME;SKILL 1;JOE;XML 1;JOE;JAVA 1;JOE;ORACLE 2;JOHN;JAVA 2;JOHN;API 我需要一个能给我这个结构的计数器: ID;
我有一个数据库,其中所有内容都与外键链接,因此 Postgres 确切地知道数据库是如何布局的。 假设我有 Table1 和 Table2。 Table1 有 3 个字段:RID、table2_rid
我尝试从多播流中计算 RTP 序列号并确定丢失的包。 请看下面的C代码: int sock, bytesRead; char buffer[MAXBUFSIZE]; uint16_t seq = 0;
我正在写一篇关于 TLS 的大学论文,有人问我为什么 TLS 序列号计数器是 64 位数字,而 TLS 在其消息中仅使用 32 位序列号。我环顾四周,甚至检查了 RFC,但到目前为止我一无所获。谁能帮
我正在尝试从 Android TV 添加 Chromecast 以转换开发设备列表,但是... 在尝试获取设备序列号时,电视上显示“0123456789ABCDEF”。我试图将它添加到列表中,但随后我
为什么我们需要TCP头中的序列号和下一个序列号字段? 下面是使用 wireshark 捕获的数据包中的 TCP header 。 最佳答案 首先,Wireshark 中 [brackets] 中的字段
我正在为我编写的软件编写自己的序列号验证/保护。 假设序列号验证器正在使用模式匹配...一旦序列号被验证,我如何更改程序本身以便它不再要求用户提供序列号? 我真的不想创建一个单独的许可文件。有没有办法
我想在列表中找到与正则表达式匹配的所有元素。为了减少正则表达式匹配的次数,我通过连接以空格分隔的元素创建了一个字符串,如下所示: list_a = ["4123", "7648", "afjsdn",
我们目前使用Rad Studio 2007,并希望很快升级到Delphi XE! 有人知道我们如何查看安装 Rad Studio 2007 时使用的序列号吗? 问候,彼得 最佳答案 “Rad Stud
调查数据表(survey_data)的样子 groupid| res_q_1| resp_q_2 -------|--------|---------- 12 | 1 |61 12
如何以编程方式获取 Google Glass 的 14 字符字母数字(如 LGGXXXXXXXXXXX)序列号? 最佳答案 看起来您可以通过检索值 android.os.Build.SERIAL 轻松
我正在开发一个项目,发送串行数据来控制 LED 灯的动画,这需要与动画引擎保持同步。似乎有一个大的串行写入缓冲区(OSX(POSIX)+ FTDI 芯片组 USB 串行设备),因此无需手动限制对 wr
我正在 Linux 下编写 C/C++ 客户端-服务器程序。假设一条消息m要从客户端发送到服务器。 Is it possible for the client to read the TCP sequ
方法一: 在给出的输入CD-KEY(序列号)的界面中,输入你已经安装的windows server 2003 的CD-KEY(序列号)即可以继续安装,而不是SQL 2000的CD-KEY
我想将xml数据存储到hive表中,XML数据: 1266 /: 61%used(9714MB/15975MB) ( / Disk Usage zab
我有一个遗留表,其自然键的一部分是一个名为 _IDENTIFIER 的列创建一个名为 _ID 的代理键似乎会让人感到困惑或 ID所以我倾向于将其命名为 SURROGATE_KEY .我所有其他表都使用
这个问题在这里已经有了答案: How can I get hardware ids/serial numbers through command prompt? (1 个回答) 关闭 9 年前。 我
我想用 C# 做一点许可证管理。 使用普通 PC 镜像,我设置了一些没有序列号的工作站,然后我想使用 C# 守护程序更改序列号。 我的问题:如何使用 C# 更改序列号并激活 Windows 7? 谢谢
我是一名优秀的程序员,十分优秀!