I need something like this for my code: Split python string every nth character?
In my case however; n are numbers within nested lists, and the strings I want to split are also within nested lists.
myList = [["'hello''my name'"],["'is Michael'"],["'and'", "'I like''apples'"]]
nList = [[7,9],[12],[5,8,8]
I want to get something like this:
myNewList = [["'hello'","'my name'"],["'is Michael'"],["'and'", "'I like'","'apples"]]
i.e I want to split the string by lengths corresponding to the numbers in nList
.
I tried using a similar solution to the link I posted above:
My attempt:
myNewList = [myList[sum(nList[:i]):sum(nList[:i+1])] for i in range(len(nList))]
but it doesn't really match my case.
EDIT:
Note, I do not want to use split
after each quote, however it is acceptable to offer it as a solution. The numbers vary and this is a simplified scenario that I am using to allude to my situation with XML data handling/writing.