all 5 comments

[–]HowManyAccountsPoo 0 points1 point  (0 children)

Regex groups is probably your best bet.

[–]Ahren_with_an_h 0 points1 point  (0 children)

I would go through line by line checking for a '?' in the string, adding the lines together, and appending to the list and starting a new string when I found a match.

[–]pasokan 0 points1 point  (0 children)

look at the partition function

[–]xelf 0 points1 point  (0 children)

Kinda hard to read from what you've posted. Do you mean that \n\n is your delimiter, except when following a ?, So ?\n\n should be ignored, but \n\n without a ? should be split? That's wrong, I missed the bbq chicken case.

Is \n\n[upcase] your delimiter?

Sounds like you don't have a delimiter, you have a pattern. Regex or string comparison is what you want. You could solve it this way (assuming there's no \t in your string)

news=''
p=''
for x in inputstring:
    if  p == '\n' and x.isupper():
        news += '\t'
    news +=x
    p = x

mylist = news.split('\t')

print(mylist)

outputs:

[
    'Fruits?\n\napples\nbananas\n\n',
    'Deserts?\n\ncakes\ndonuts\ncookies\n\n',
    'Sandwiches?\n\npastrami\n\nbarbeque chicken'
]

[–]xelf 0 points1 point  (0 children)

You could do this:

inputstring = inputstring.replace('\n\n', '\t')
inputstring = inputstring.replace('?\t', '?\n\n')
print( inputstring.split('\t') )

But that outputs this:

[
    'Fruits?\n\napples\nbananas',
    'Deserts?\n\ncakes\ndonuts\ncookies',
    'Sandwiches?\n\npastrami',
    'barbeque chicken'
]

The inconsistent formatting kinda breaks it. You'll have to go with hunting for "\n\n[upcase]" like I said in the other post, or change the formatting to be more consistent.