开发者

Multiple common prefixes among different paths

Sorry for the title, my problem is as follows. I have a list of paths and I want to get multiple common prefixes. For example, given I have:

['/usr/local/lib/python2.7/dist-packages/pkg_name-0.1-py2.7.egg/pkg_name开发者_运维百科',
 '/usr/local/lib/python2.7/dist-packages/pkg_name-0.1-py2.7.egg/EGG-INFO',
 '/usr/bin/pkg_name']

I want to have:

['/usr/local/lib/python2.7/dist-packages/pkg_name-0.1-py2.7.egg/',
 '/usr/bin/pkg_name']

Because the first two have a common prefix that is a directory. Hope I made this clear,

rubik

EDIT: The paths I have are Python eggs and some executables. I want to remove the entire egg, not the directories inside it, like EGG-INFO or pkg_name. So it has to be /usr/.../dist-packages/pkg_name-0.1-py2.7.egg/. The other path, since it is an executable remains as it is.

Thank you


The problem is not well-defined. What do you want to have in this case:

/usr/bin/a
/usr/bin/b
/usr/etc
/usr/local

Shall it be one /usr or two: /usr/bin /usr, or three?

In either case, the algorithm will be like this:

  1. sort the list
  2. take the first element and do os.path.commonprefix() with 2nd, 3rd, ..., i-th until common prefix is not /; that will be your first group
  3. repeat step 2, starting from (i+1)th
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜