Search code examples
pythonfilternumbers

Extract numbers from an Array which has more than one string element


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’]

Solution

  • 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]