How to group similar items in a list?

后端 未结 1 352
野的像风
野的像风 2021-01-11 10:03

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\',         


        
相关标签:
1条回答
  • 2021-01-11 10:43

    The .split("_")[0] part should be inside a single-argument function that you pass as the second argument to itertools.groupby.

    >>> import os, itertools
    >>> test = ['abc_1_2', 'abc_2_2', 'hij_1_1', 'xyz_1_2', 'xyz_2_2']
    >>> [list(g) for _, g in itertools.groupby(test, lambda x: x.split('_')[0])]
    [['abc_1_2', 'abc_2_2'], ['hij_1_1'], ['xyz_1_2', 'xyz_2_2']]
    >>>
    

    Having it in the for ... part does nothing since the result is immediately discarded.


    Also, it would be slightly more efficient to use str.partition when you only want a single split:

    [list(g) for _, g in itertools.groupby(test, lambda x: x.partition('_')[0])]
    

    Demo:

    >>> from timeit import timeit
    >>> timeit("'hij_1_1'.split('_')")
    1.3149855638076913
    >>> timeit("'hij_1_1'.partition('_')")
    0.7576401470019234
    >>>
    

    This isn't a major concern as both methods are pretty fast on small strings, but I figured I'd mention it.

    0 讨论(0)
提交回复
热议问题