grouping a list into sublists, breaked by alphabet elements in python

1581 views python
1

I have a list in python that is mixed: some elements are numeric and some are alphabet.

For example: l = ['999','123','hello','222','333','444','bye']

I want to split this list to a lists that are seperated by the elements that are all alphabet:

['999','123','hello'], ['222','333','444','bye']

For ['hello', '123', 'test', 'test', '456', 'test', '789'] The output will be: ['hello'],['123','test'],['test'],['456','test'],['789']

Every element is all alphabet or all numeric.

What is the most pythonic way to do so?

answered question

If you had an additional bye at the end of your input... would the second list output have the additional bye as well or something else?

So you want to split when you encounter any string that contains at least one symbol that is not a number?

@JonClements - Yes

@oren_isp what would ['hello', '123', 'test', 'test', '456', 'test', '789'] be?

@not_a_bot_no_really_82353 - Yes, but preferably in the most pythonic and fastest way since I have to go over A LOT of data

pythonic and fastest does not necessarily mean most efficient. You probably have to try different methods and benchmark them.

@JonClements sorry I misread you question, in that case the output will be ['hello'],['123','test'],['test'],['456','test'],['789']

@oren_isp also - your title and question body seem to be asking different things... your title is asking for indeces of elements, but it seems to you're after grouping a list into sublists based on criteria... you might want to edit your question to be consistent.

@oren_isp yeah... that doesn't seem quite consistent with the logic based on your input and my original question... you probably want to detail out the rules for grouping in your question as well...

@JonClements Why not? anyway I added this example to the post.. So do you know what will be the best solution here? :)

1 Answer

8

output = []
for i in l:
    if not output or output[-1][-1].isalpha():
        output.append([i])
    else:
        output[-1].append(i)

so that with:

l = ['999','123','hello','222','333','444','bye']

output would become:

[['999', '123', 'hello'], ['222', '333', '444', 'bye']]

or with:

l = ['hello', '123', 'test', 'test', '456', 'test', '789']

output would become:

[['hello'], ['123', 'test'], ['test'], ['456', 'test'], ['789']]

posted this

Have an answer?

JD

Please login first before posting an answer.