How to group similar items in a list?
问题 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