I’ve got a list of strings that I split into a list of list on (‘/>’), yet I want to split the elements of that list within further depending on IF it has a certain character (‘/’) in it while maintaining the order that the ‘further elements’ are split in. So for example
lol=['FruitSalad://Fruits/Seasonal/Summer/>June>July>August>Mangoes/Grapes/Guava', 'FruitSalad://Fruits/Seasonal/Winter/>Nov>Dec>Jan>Banana/Oranges', 'FruitSalad://Fruits/Seasonal/Summer/>June>July>August>Pineapple'] l1=[lol[_].split('/>')[-1].split('>') for _ in range(len(lol)) if '/>' in lol[_]] >>> l1 [['June', 'July', 'August', 'Mangoes/Grapes/Guava'], ['Nov', 'Dec', 'Jan', 'Banana/Oranges'], ['June', 'July', 'August', 'Pineapple']]
Instead of the above, I’m trying to get
[['June', 'July', 'August', 'Mangoes', 'Grapes', 'Guava'], ['Nov', 'Dec', 'Jan', 'Banana', 'Oranges'], ['June', 'July', 'August', 'Pineapple']]
Any help is greatly appreciated! Please let me know if my question doesn’t make sense. Not sure if this matters but this should work on python 2.7 and 3.6. Thank You!
Advertisement
Answer
- for each element, split on
/>
- Take the final element and replace the slash with
>
and then split again
[l.split("/>",1)[-1].replace("/", ">").split(">") for l in lol]