I am looking to group similar items in a list based on the first three characters in the string. For example:
test = ['abc_1_2', 'abc_2_2', 'hij_1_1', 'xyz_1_2', 'xyz_2_2']
How can I group the above list items into groups based on the first grouping of letters (e.g. 'abc'
)? The following is the intended output:
output = {1: ('abc_1_2', 'abc_2_2'), 2: ('hij_1_1',), 3: ('xyz_1_2', 'xyz_2_2')}
or
output = [['abc_1_2', 'abc_2_2'], ['hij_1_1'], ['xyz_1_2', 'xyz_2_2']]
I have tried using itertools.groupby
to accomplish this without success:
>>> import os, itertools
>>> test = ['abc_1_2', 'abc_2_2', 'hij_1_1', 'xyz_1_2', 'xyz_2_2']
>>> [list(g) for k.split("_")[0], g in itertools.groupby(test)]
[['abc_1_2'], ['abc_2_2'], ['hij_1_1'], ['xyz_1_2'], ['xyz_2_2']]
I have looked at the following posts without success:
How to merge similar items in a list. The example groups similar items (e.g. 'house'
and 'Hose'
) using an approach that is overly complicated for my example.
How can I group equivalent items together in a Python list?. This is where I found the idea for the list comprehension.