我有4个元素存储在一个数组中,我只想从提到Approved个关键字的字符串中获取整数.

my_array = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 25000 Regular 10/20/2023 Approved 4/28/2022',
            'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 5000 Regular 10/19/2023 Closed 4/27/2022',
            'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 15000 Regular 10/19/2023 Closed 4/27/2022',
            'STK722222 4/26/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 10000 Regular 10/18/2023 Approved 4/26/2022']

到目前为止,我只能通过以下操作从列表中获取整数:

import re

# Print integers
nums = [int(re.search(r'\d+000', s)[0]) for s in my_array]
print(nums)

# Printed output:
# [25000, 5000, 15000, 10000]

预期输出为:

[25000,10000]

推荐答案

list comprehensionre.searchif一起使用.请注意,第二个示例显示,基于regex的搜索可以非常强大地提取出您想要的模式,因此我几乎总是喜欢它而不是精确的字符串匹配(除非性能非常关键).此外,我将array重命名为lst(该数据 struct 在Python中称为list,而array是其他一些语言).

import re

my_lst = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 25000 Regular 10/20/2023 Approved 4/28/2022',
          'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 5000 Regular 10/19/2023 Closed 4/27/2022',
          'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 15000 Regular 10/19/2023 Closed 4/27/2022',
          'STK722222 4/26/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 10000 Regular 10/18/2023 Approved 4/26/2022']

nums = [int(re.search(r'\d+000', s)[0]) for s in my_lst if re.search(r'Approved', s)]
print(nums)
# [25000, 10000]

nums = [int(re.search(r'\d+000', s)[0]) for s in my_lst if re.search(r'4/2[67]', s)]
print(nums)
# [5000, 15000, 10000]

Python相关问答推荐

根据网格和相机参数渲染深度

根据给定日期的状态过滤查询集

使用scipy. optimate.least_squares()用可变数量的参数匹配两条曲线

Django mysql图标不适用于小 case

为什么带有dropna=False的groupby会阻止后续的MultiIndex.dropna()工作?

Pandas 都是(),但有一个门槛

从numpy数组和参数创建收件箱

pandas滚动和窗口中有效观察的最大数量

图像 pyramid .难以创建所需的合成图像

为什么抓取的HTML与浏览器判断的元素不同?

索引到 torch 张量,沿轴具有可变长度索引

如何从需要点击/切换的网页中提取表格?

* 动态地 * 修饰Python中的递归函数

PYTHON、VLC、RTSP.屏幕截图不起作用

使用字典或列表的值组合

如何在Airflow执行日期中保留日期并将时间转换为00:00

使用SQLAlchemy从多线程Python应用程序在postgr中插入多行的最佳方法是什么?'

Python:从目录内的文件导入目录

修改.pdb文件中的值并另存为新的

无法在盐流道中获得柱子