0

I am trying to do the following:

  1. create an array of random data
  2. create an array of predefined codes (AW, SS)
  3. subtract all numbers as well as any instance of predefined code.
  4. if a string called "HL" remains after step 3, remove that as well and take the next alphabet pair. If a string called "HL" is the ONLY string in the array then take that.

I do not know how to go about completing steps 3 - 4.

1.

array_data = ['HL22','PG1234-332HL','1334-SF-21HL','HL43--222PG','HL222AW11144RH','HLSSDD','SSDD']

2.

predefined_code = ['AW','SS']

3.

ideally, results for this step will look like

result_data = [['HL'],['PG,HL'],['SF','HL'],['HL','PG'],['HL','RH'], 
['HL','DD'],['DD']

4. ideally, results for this step will look like this:

result_data = [['HL'],['PG'],['SF'],['PG'],['RH'], ['DD'],['DD']

for step 3, I have tried the following code

not_in_predefined = [item for item in array_data if item not in predefined_code]

but this doesnt produce the result im looking for, because it it checking item against item. not a partial string match.

3
  • Step 3 is pretty much where actual coding begins. What have you tried so far? Commented Oct 15, 2018 at 1:33
  • updated post thank you Commented Oct 15, 2018 at 1:42
  • Sorry, what is difference with this your question ? Commented Oct 15, 2018 at 5:30

1 Answer 1

1

This is fairly simple using Regex.

re.findall(r'[A-Z].',item) should give you the text from your strings, and then you can do the required processing on that.

You may want to convert the list to a set eventually and use the difference operation, instead of looping and removing the elements defined in the predefined_code list.

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.