I have several Python lists in the following format:
rating = ['What is your rating for?: Bob', 'What is your rating for?: Alice', 'What is your rating for?: Mary Jane']
opinion = ['What is your opinion of?: Bob', 'What is your opinion of?: Alice', 'What is your opinion of?: Mary Jane']
I am trying to write a function that will evaluate a given list and generate two data structures from it:
- a list of the names that appear after the colons (:)
- a string variable that has the text that is repeated before the colons (:)
Ideally, both items would be named based off of the original list name. Also, the delimiter and the first space after it should be ignored.
Desired sample output for the two above examples:
rating_names = ['Bob', 'Alice', 'Mary Jane']
rating_text = 'What is your rating for?'
opinion_names = ['Bob', 'Alice', 'Mary Jane']
opinion_text = 'What is your opinion of?'
I've been able to make this work for a single list by removing a fixed string from each list item, but haven't quite figured out how to make it work for a variable number of characters before the delimiter and the potential of a two word name (e.g. 'Mary Jane') after it.
rating_names = ([s.replace('What is your rating for?': ','') for s in rating])
After searching, it appears that a regular expression like look-ahead (1, 2) might be the solution, but I can't get that to work, either.