I'm trying to figure out how to split the following list into separate lists based on a character in the list.
list = ['@', '2014', '00:03:01', 'Matt', '"login"', '0.01', '@', '2014', '02:06:12', 'Mary', '"login"', '0.01']
I want to create a list after every "@" symbol is introduced. For example, I would want the output to be the following:
NewList1 = ['@', '2014', '00:03:01', 'Matt', '"login"', '0.01']
NewList2 = ['@', '2014', '02:06:12', 'Mary', '"login"', '0.01']
Is this possible?
You could use itertools.groupby:
import itertools as IT
import operator
seq = ['@', '2014', '00:03:01', 'Matt', '"login"', '0.01', '@', '2014', '02:06:12', 'Mary', '"login"', '0.01']
groups = (list(g) for k,g in IT.groupby(seq, lambda item: item=='@'))
print(list(IT.starmap(operator.add, IT.izip(*[groups]*2))))
prints
[['@', '2014', '00:03:01', 'Matt', '"login"', '0.01'], ['@', '2014', '02:06:12', 'Mary', '"login"', '0.01']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With