Skip to content

Instantly share code, notes, and snippets.

@catichenor
Created August 13, 2018 23:09
Show Gist options
  • Save catichenor/f671e507f759a7a9103431fa9e168b60 to your computer and use it in GitHub Desktop.
Save catichenor/f671e507f759a7a9103431fa9e168b60 to your computer and use it in GitHub Desktop.
Takes in a list of strings containing numerically ordered lists and fixes their numbering
import re
step_list = ['1. Pour\n2. Lather\n2. Rinse\n3. Repeat', # Note two step 2's.
'1. Start filling with water\n1. Open Lid\n1. Load\n2. Add detergent\n3. Add fabric softener\n4. Add clothes\n5. Close lid', # Note 3 step 1's
'1. Put toothpaste on brush\n2. Rub toothbrush on all teeth for two minutes, spit as necessary\n3. Rinse mouth\n4. Floss\n5. Swish mouthwash in mouth for 30 seconds\n6. Gargle, spit']
new_step_list = []
for row in step_list:
if re.match(r'^\d+\. ', row):
new_steps = []
idx = 1
for line in row.splitlines():
new_steps.append(re.sub(r'^\d+', str(idx), line))
idx += 1
out_item_content = '\n'.join(new_steps)
new_step_list.append(out_item_content)
new_step_list
# `new_step_list` should now contain:
# ['1. Pour\n2. Lather\n3. Rinse\n4. Repeat',
# '1. Start filling with water\n2. Open Lid\n3. Load\n4. Add detergent\n5. Add fabric softener\n6. Add clothes\n7. Close lid',
# '1. Put toothpaste on brush\n2. Rub toothbrush on all teeth for two minutes, spit as necessary\n3. Rinse mouth\n4. Floss\n5. Swish mouthwash in mouth for 30 seconds\n6. Gargle, spit']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment