11

I have for example the following list:

['|', u'MOM', u'DAD', '|', u'GRAND', '|', u'MOM', u'MAX', u'JULES', '|']

and want it to be split by the "|" so the result would look like:

[[u'MOM', u'DAD'],[ u'GRAND'], [u'MOM', u'MAX', u'JULES']]

How can I do this? I only find examples of sublists on the net which need a length of the elements

4 Answers 4

15
>>> [list(x[1]) for x in itertools.groupby(['|', u'MOM', u'DAD', '|', u'GRAND', '|', u'MOM', u'MAX', u'JULES', '|'], lambda x: x=='|') if not x[0]]
[[u'MOM', u'DAD'], [u'GRAND'], [u'MOM', u'MAX', u'JULES']]
Sign up to request clarification or add additional context in comments.

4 Comments

This solution for me is full of magic but works perfect! Thank you
Read the groupby documentation for a start. :-)
The full line is, for clarity: [list(x[1]) for x in itertools.groupby(myList, lambda x: x=='|') if not x[0]]
@ninjagecko: That's why I split out the list and the lambda in my answer.
10

itertools.groupby() does this very nicely...

>>> import itertools
>>> l = ['|', u'MOM', u'DAD', '|', u'GRAND', '|', u'MOM', u'MAX', u'JULES', '|']
>>> key = lambda sep: sep == '|'
>>> [list(group) for is_key, group in itertools.groupby(l, key) if not is_key]
[[u'MOM', u'DAD'], [u'GRAND'], [u'MOM', u'MAX', u'JULES']]

1 Comment

This is more readable than the accepted answer. To the top you go!
1

Simple solution using plain old for-loop (was beaten to it for the groupby solution, which BTW is better!)

seq = ['|', u'MOM', u'DAD', '|', u'GRAND', '|', u'MOM', u'MAX', u'JULES', '|']

S=[]
tmp=[]

for i in seq:
    if i == '|':
        S.append(tmp)
        tmp = []
    else:
        tmp.append(i)

# Remove empty lists
while True:
    try:
        S.remove([])
    except ValueError:
        break

print S

Gives

[[u'MOM', u'DAD'], [u'GRAND'], [u'MOM', u'MAX', u'JULES']]

Comments

0
>>> reduce(
        lambda acc,x: acc+[[]] if x=='|' else acc[:-1]+[acc[-1]+[x]], 
        myList,
        [[]]
    )
[[], ['MOM', 'DAD'], ['GRAND'], ['MOM', 'MAX', 'JULES'], []]

Of course you'd want to use itertools.groupby, though you may wish to note that my approach "correctly" puts empty lists on the ends. =)

2 Comments

When you say "correctly", how does that match with the desired output from the question or the linked article?
@Johnsyweb I am well aware. Nevertheless the semantics of the specification however are inelegant and should be equivalent to [].split(token); the input should just leave off the '|' on the ends.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.