从具有多个字符串元素的数组中提取数字

发布于 2025-02-08 12:12:47 字数 760 浏览 1 评论 0原文

一个阵列有4个元素。我只想打印出数千个

my_array = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 5000 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 Approved 4/27/2022',
            'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 15000 Regular 10/19/2023 Approved 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']

预期输出为:

[‘5000’, ‘5000’, ‘15000’, ‘10000’]

An array has 4 elements. I want only the the thousands to printed out.

my_array = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 5000 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 Approved 4/27/2022',
            'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 15000 Regular 10/19/2023 Approved 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']

Expected output is:

[‘5000’, ‘5000’, ‘15000’, ‘10000’]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

£冰雨忧蓝° 2025-02-15 12:12:47

使用 re.search ,将第一匹匹配到1个或更多位数的模式,然后是3个零。

import re

my_array = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 5000 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 Approved 4/27/2022',
            'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 15000 Regular 10/19/2023 Approved 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']

# If you want strings:
nums = [re.search(r'\d+000', s)[0] for s in my_array]
print(nums)
# ['5000', '5000', '15000', '10000']

# If you want integers:
nums = [int(re.search(r'\d+000', s)[0]) for s in my_array]
print(nums)
# [5000, 5000, 15000, 10000]

Use re.search, which extract the first match to the pattern of 1 or more digit, followed by 3 zeros.

import re

my_array = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 5000 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 Approved 4/27/2022',
            'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 15000 Regular 10/19/2023 Approved 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']

# If you want strings:
nums = [re.search(r'\d+000', s)[0] for s in my_array]
print(nums)
# ['5000', '5000', '15000', '10000']

# If you want integers:
nums = [int(re.search(r'\d+000', s)[0]) for s in my_array]
print(nums)
# [5000, 5000, 15000, 10000]
垂暮老矣 2025-02-15 12:12:47

尝试以下操作:

lines = [l.split() for l in my_array] 
[w for words in lines for w in words if w.isnumeric() and int(w) > 1000]
['5000', '5000', '15000', '10000']

Try this:

lines = [l.split() for l in my_array] 
[w for words in lines for w in words if w.isnumeric() and int(w) > 1000]
['5000', '5000', '15000', '10000']
春风十里 2025-02-15 12:12:47

您可以映射一个功能,该函数将首先分配为字符串并在所需的数组中获取项目,在这种情况下:

>>> array = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 5000 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 Approved 4/27/2022',
...     'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 15000 Regular 10/19/2023 Approved 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']
>>> #mapping with the function
>>> thousands = list(map(lambda x:x.split()[-5],array))
>>> thousands
['5000', '5000', '15000', '10000']

you could map a function that, splits the first the string and taking the item in the array that you want, in this case:

>>> array = ['STK72184 4/28/2022 50 from Exchange Balance, 50 from Earning Balance & 10 from Bonus 5000 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 Approved 4/27/2022',
...     'STK725721 4/27/2022 50 from Exchange Balance, 40 from Earning Balance & 10 from Bonus Balance 15000 Regular 10/19/2023 Approved 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']
>>> #mapping with the function
>>> thousands = list(map(lambda x:x.split()[-5],array))
>>> thousands
['5000', '5000', '15000', '10000']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文