Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a list based on a delimiter word

Tags:

I have a list containing various string values. I want to split the list whenever I see WORD. The result will be a list of lists (which will be the sublists of original list) containing exactly one instance of the WORD I can do this using a loop but is there a more pythonic way to do achieve this ?

Example = ['A', 'WORD', 'B' , 'C' , 'WORD' , 'D']

result = [['A'], ['WORD','B','C'],['WORD','D']]

This is what I have tried but it actually does not achieve what I want since it will put WORD in a different list that it should be in:

def split_excel_cells(delimiter, cell_data):      result = []      temp = []      for cell in cell_data:         if cell == delimiter:             temp.append(cell)             result.append(temp)             temp = []         else:             temp.append(cell)      return result 
like image 695
Cemre Mengü Avatar asked Mar 12 '13 09:03

Cemre Mengü


People also ask

How do you split a list with delimiter?

Use split() method to split by delimiter. If the argument is omitted, it will be split by whitespace, such as spaces, newlines \n , and tabs \t . Consecutive whitespace is processed together. A list of the words is returned.

Can you use the split function with a list?

Python String split() MethodThe split() method splits a string into a list. You can specify the separator, default separator is any whitespace.

How do you split elements in a list?

To split the elements of a list in Python: Use a list comprehension to iterate over the list. On each iteration, call the split() method to split each string. Return the part of each string you want to keep.

How do you split a list into words in Python?

Use the list() class to split a word into a list of letters, e.g. my_list = list(my_str) . The list() class will convert the string into a list of letters.


2 Answers

import itertools  lst = ['A', 'WORD', 'B' , 'C' , 'WORD' , 'D'] w = 'WORD'  spl = [list(y) for x, y in itertools.groupby(lst, lambda z: z == w) if not x] 

this creates a splitted list without delimiters, which looks more logical to me:

[['A'], ['B', 'C'], ['D']] 

If you insist on delimiters to be included, this should do the trick:

spl = [[]] for x, y in itertools.groupby(lst, lambda z: z == w):     if x: spl.append([])     spl[-1].extend(y) 
like image 141
georg Avatar answered Oct 29 '22 21:10

georg


I would use a generator:

def group(seq, sep):     g = []     for el in seq:         if el == sep:             yield g             g = []         g.append(el)     yield g  ex = ['A', 'WORD', 'B' , 'C' , 'WORD' , 'D'] result = list(group(ex, 'WORD')) print(result) 

This prints

[['A'], ['WORD', 'B', 'C'], ['WORD', 'D']] 

The code accepts any iterable, and produces an iterable (which you don't have to flatten into a list if you don't want to).

like image 45
NPE Avatar answered Oct 29 '22 21:10

NPE