0

I have a list named 'exemptions' with several fields (string variables).

exemptions = ['S-1', '20090820', '\t\t\t\tDOLLAR GENERAL CORP', '\t\t0000029534', 'S-1/A', '20021114', '\t\t\t\tCONSTAR INTERNATIONAL INC', '\t\t0000029806', '\t\t\t\tCONSTAR FOREIGN HOLDINGS INC', '\t\t0001178543', '\t\t\t\tCONSTAR PLASTICS LLC', '\t\t0001178541', '\t\t\t\tDT INC', '\t\t0001178539', '\t\t\t\tBFF INC', '\t\t0001178538', '\t\t\t\tCONSTAR INC', '\t\t0001178537', 'S-1', '20020523', '\t\t\t\tCONSTAR INTERNATIONAL INC', '\t\t0000029806', 'S-1', '20051123', '\t\t\t\tEXCO RESOURCES INC', '\t\t0000316300', 'S-1', '20061221', '\t\t\t\tEXCO RESOURCES INC', '\t\t0000316300', 'S-1/A', '20140327', '\t\t\t\tAlly Financial Inc.', '\t\t0000040729', 'S-1', '20110331', '\t\t\t\tAlly Financial Inc.', '\t\t0000040729', 'S-1', '20040319', '\t\t\t\tDIGIRAD CORP', '\t\t0000707388', 'S-1', '20040408', '\t\t\t\tBUCYRUS INTERNATIONAL INC', '\t\t0000740761', 'S-1', '20041027', '\t\t\t\tBUCYRUS INTERNATIONAL INC', '\t\t0000740761', 'S-1', '20050630', '\t\t\t\tSEALY CORP', '\t\t0000748015', 'S-1', '20140512', '\t\t\t\tCITIZENS FINANCIAL GROUP INC/RI', '\t\t0000759944']

I would like to create sublists at the beginning of every 'S-1' or 'S-1/A'. Desired output would be:

exemptions = [['S-1', '20090820', '\t\t\t\tDOLLAR GENERAL CORP', '\t\t0000029534'], ['S-1/A', '20021114', '\t\t\t\tCONSTAR INTERNATIONAL INC', '\t\t0000029806', '\t\t\t\tCONSTAR FOREIGN HOLDINGS INC', '\t\t0001178543', '\t\t\t\tCONSTAR PLASTICS LLC', '\t\t0001178541', '\t\t\t\tDT INC', '\t\t0001178539', '\t\t\t\tBFF INC', '\t\t0001178538', '\t\t\t\tCONSTAR INC', '\t\t0001178537'], ['S-1', '20020523', '\t\t\t\tCONSTAR INTERNATIONAL INC', '\t\t0000029806'], ['S-1', '20051123', '\t\t\t\tEXCO RESOURCES INC', '\t\t0000316300'], ['S-1', '20061221', '\t\t\t\tEXCO RESOURCES INC', '\t\t0000316300'], ['S-1/A', '20140327', '\t\t\t\tAlly Financial Inc.', '\t\t0000040729'], ['S-1', '20110331', '\t\t\t\tAlly Financial Inc.', '\t\t0000040729'], ['S-1', '20040319', '\t\t\t\tDIGIRAD CORP', '\t\t0000707388'], ['S-1', '20040408', '\t\t\t\tBUCYRUS INTERNATIONAL INC', '\t\t0000740761'], ['S-1', '20041027', '\t\t\t\tBUCYRUS INTERNATIONAL INC', '\t\t0000740761'], ['S-1', '20050630', '\t\t\t\tSEALY CORP', '\t\t0000748015'], ['S-1', '20140512', '\t\t\t\tCITIZENS FINANCIAL GROUP INC/RI', '\t\t0000759944']]

I tried _list = [i.split('S-1') for i in exemptions], but does not give me what I need...

Any suggestion? Thank you so much

1
  • what if exemptions list starts with another letter not S ? Commented Aug 16, 2019 at 15:38

3 Answers 3

1

Join the list as a string with custom delimiter, say | for example, use re.split to split on every occurrence of S-1 and then split each element of the resulting list back to a list based on delimiter |

>>> res = [s.strip('|').split('|') for s in re.split(r'(?=S-1)', '|'.join(exemptions)) if s]
>>>
>>> pprint(res)
[['S-1', '20090820', '\t\t\t\tDOLLAR GENERAL CORP', '\t\t0000029534'],
 ['S-1/A',
  '20021114',
  '\t\t\t\tCONSTAR INTERNATIONAL INC',
  '\t\t0000029806',
  '\t\t\t\tCONSTAR FOREIGN HOLDINGS INC',
  '\t\t0001178543',
  '\t\t\t\tCONSTAR PLASTICS LLC',
  '\t\t0001178541',
  '\t\t\t\tDT INC',
  '\t\t0001178539',
  '\t\t\t\tBFF INC',
  '\t\t0001178538',
  '\t\t\t\tCONSTAR INC',
  '\t\t0001178537'],
 ['S-1', '20020523', '\t\t\t\tCONSTAR INTERNATIONAL INC', '\t\t0000029806'],
 ['S-1', '20051123', '\t\t\t\tEXCO RESOURCES INC', '\t\t0000316300'],
 ['S-1', '20061221', '\t\t\t\tEXCO RESOURCES INC', '\t\t0000316300'],
 ['S-1/A', '20140327', '\t\t\t\tAlly Financial Inc.', '\t\t0000040729'],
 ['S-1', '20110331', '\t\t\t\tAlly Financial Inc.', '\t\t0000040729'],
 ['S-1', '20040319', '\t\t\t\tDIGIRAD CORP', '\t\t0000707388'],
 ['S-1', '20040408', '\t\t\t\tBUCYRUS INTERNATIONAL INC', '\t\t0000740761'],
 ['S-1', '20041027', '\t\t\t\tBUCYRUS INTERNATIONAL INC', '\t\t0000740761'],
 ['S-1', '20050630', '\t\t\t\tSEALY CORP', '\t\t0000748015'],
 ['S-1',
  '20140512',
  '\t\t\t\tCITIZENS FINANCIAL GROUP INC/RI',
  '\t\t0000759944']]
>>> 
Sign up to request clarification or add additional context in comments.

Comments

0

Does this work?

exemptions = ['S-1', '20090820', .... , '\t\t0000759944']
result = []
for e in exemptions:
    if e in ("S-1", "S-1/A"):
        result.append([])
    result[-1].append(e)

Note that this relies on the fact that your input list starts with a 'starting' S-1 value, each time it encouters one of those it adds a new sublist to the end of result. Then all you need to do is keep adding values onto the end of the last sublist.

1 Comment

It also doesn't do what the OP asked for if there's a value like "this is NOT an S-1, it's actually an F-4".
0
# exemptions is input list
finalList = []
temporaryList = []
for eachItem in exemptions:
    if 'S-1' in eachItem:
        temporaryList = []
        temporaryList.append(eachItem)
    else:
        temporaryList.append(eachItem)
finalList.append(temporaryList)

print finalList

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.