Group files based on common prefix
Consider the following set of files:
/wr_vjxeacn/lzx/vjx/rkkelkwrvkjl.o /wr_vjxeacn/lzx/vjx/wllnxncvr.o /wr_hvlx/lzx/hvlx/wlxkjjlnr/Sbisln.xww /wr_hvlx/lzx/wllqepse/lzx/xww/ANTLR/evi /wr_hvlx/lzx/wllqepse/lzx/xww/ANTLR/zajrvhn/sjrez3x.cee /wr_hvlx/lzx/wllqepse/lzx/xww/ivj/GNUhstnmven /wr_hvlx/eklr+mkajc/sjrez3x64.evi.7ss153m930724031i252iic841n68i6i /wr_hvlx/lzx/wllqepse/lzx/xww/ANTLR/evi/sjrez3x.evi /wnkwenrkkel/lzx /wnkwenrkkel/lzx/GNUhstnmven.xkhhkj /wnkwenrkkel/lzx/GNUhstnmven.cnwl /wnkwenrkkel/lzx/GNUhstnmven.evlr /wnkwenrkkel/lzx/GNUhstnmven.gvjckgl-vs32
What I was trying to figure out is the optimal way to group items with a common dirname prefix
common prefix dirname = os.path.dirname(os.path.commonprefix(...))
So, ideally, after grouping, the above should look like
/wr_vjxeacn/lzx/vjx
/wr_vjxeacn/lzx/vjx/rkkelkwrvkjl.o
/wr_vjxeacn/lzx/vjx/wllnxncvr.o
*************************************************************
/wr_hvlx/lzx/hvlx
/wr_hvlx/lzx/hvlx/wlxkjjlnr/Sbisln.xww
*************************************************************
/wr_hvlx/lzx/wllqepse
/wr_hvlx/lzx/wllqepse/lzx/xww/ANTLR/evi
/wr_hvlx/lzx/wllqepse/lzx/xww/ANTLR/zajrvhn/sjrez3x.cee
/wr_hvlx/lzx/wllqepse/lzx/xww/ivj/GNUhstnmven
*************************************************************
/wr_hvlx/eklr+mkajc
/wr_hvlx/eklr+mkajc/sjrez3x64.evi.7ss153m930724031i252iic841n68i6i
*************************************************************
/wr_hvlx/lzx/wllqepse/lzx/xww/ANTLR/evi
/wr_hvlx/lzx/wllqepse/lzx/xww/ANTLR/evi/sjrez3x.evi
*************************************************************
/wnkwenrkkel
/wnkwenrkkel/lzx
*************************************************************
/wnkwenrkkel/lzx
/wnkwenrkkel/lzx/GNUhstnmven.xkhhkj
/wnkwenrkkel/lzx/GNUhstnmven.cnwl
/wnkwenrkkel/lzx/GNUhstnmven.evlr
/wnkwenrkkel/lzx/GNUhstnmven.gvjckgl-vs32
What i tried
-
itertools.groupby
but it doesn't have lookahead or lookbehind. - iteration and prefix violation. It is not possible to find a working solution to handle all edge cases.
Motivation
I have a list of checked files that I want to group based on modules to identify the respective owners of the modules.
+3
source to share
No one has answered this question yet
See similar questions:
or similar: