Home > front end >  Splitting a string within a list into a list of lists and further splitting its elements if special
Splitting a string within a list into a list of lists and further splitting its elements if special

Time:12-30

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!

CodePudding user response:

  1. for each element, split on />
  2. Take the final element and replace the slash with > and then split again
 [l.split("/>",1)[-1].replace("/", ">").split(">") for l in lol]

CodePudding user response:

you can do the following.

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[_]]
final_l1 = []
for elem in l1:
   final_l1.append([])
   for each in elem:
      final_l1[-1]  = each.split('/')

print(print(final_l1))

output

[['June', 'July', 'August', 'Mangoes', 'Grapes', 'Guava'], ['Nov', 'Dec', 'Jan', 'Banana', 'Oranges'], ['June', 'July', 'August', 'Pineapple']]

CodePudding user response:

@Kaz Salvatore, try this:

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']

[x.replace('://', ' ')
 .replace('/', ' ')
 .replace('/>', ' ')
 .replace('>', ' ')
 .replace('  ', ' ')
 .split() for x in lol]

Output:

[['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']]

CodePudding user response:

you also can use Regular expression :

import re 
[re.split(r'/|>',l.split('/>')[-1]) for l in lol if '/>' in l]
  • Related