python - 将 finditer() 输出放入数组
问题描述
我试图在一个字符串中找到所有“TRN”的索引,我已经完成了,但是我想将所有索引放入一个数组中,我似乎做不到。
import re
string = 'a string with TRN, then another TRN'
for match in re.finditer('TRN', string):
spots = match.start()
print(spots)
输出是:
14
32
我想要的输出是:
[14, 32]
我已经尝试将它放入数组并像这样附加输出,但结果是 NONE NONE。
import re
into_array = []
string = 'a string with TRN, then another TRN'
for match in re.finditer('TRN', string):
spots = match.start()
x = into_array.append(spots)
print(x)
输出是:
None
None
任何帮助将不胜感激。
解决方案
您正在打印append
(不输出任何内容,因此None
)的输出,而不是spots
您想要的。
import re
into_array = []
string = 'a string with TRN, then another TRN'
for match in re.finditer('TRN', string):
spots = match.start()
into_array.append(spots)
print(into_array)
推荐阅读
- tfs - TFS 收集级别的自定义安全组
- android - 我想在软键盘上方创建一个视图
- amazon-web-services - 如何配置我的 EC2 实例运行 DPDK 以过滤弹性 IP 和另一个 ec2 实例之间的流量?
- python - Extracting name and number from a string
- ruby - 在 Firestore 中写入需要太多时间
- java - 如何使用 log4j 动态设置日志文件名,但在发出数据库请求之后?
- java - 为什么通知没有显示在 API 28 中?
- c - 如何将 VL53L0X 标头包含到我的 STM32L 项目中
- python - pythonscript将图片保存到文件时如何在网络服务器上显示图片?
- visual-studio-code - VSCode Jest 因 SystaxError 失败:意外的令牌 =