0

I have a list of strings, below is just one example:

str_list = ['Navitas', 'Organic Cacao Powder', '227g', 'SALE $7.49 ea', 'Regular $9:99-ea', 'Valid 01/12 - 01/18']

I want to extract the '227g' element based on finding the index.

unit_index = [idx for idx, val in enumerate(str_list) if val.endswith((' ml','-pack','{\d}g', ' L'))]

But this doesn't seem to work. I would like to find out where it ends with a digit followed by a unit of measure (ml, g, L)

1
  • Maybe just filter with re.search(r'(?: ml|-pack|\d+g)$', val)? Or see ideone.com/L3xL1L Commented Jan 24, 2022 at 0:30

1 Answer 1

2

You can use \d+\s?(ml|-pack|g|L)$ to check for a number with a unit after it.

import re

str_list = ['Navitas', 'Organic Cacao Powder', '227g', 'SALE $7.49 ea', 'Regular $9:99-ea', 'Valid 01/12 - 01/18']
r = r"\d+\s?(ml|-pack|g|L)$"
unit_index = [idx for idx, val in enumerate(str_list) if re.search(r, val)][0]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.