不同路径之间的多个公共前缀

发布于 2024-11-29 11:11:29 字数 641 浏览 0 评论 0原文

抱歉标题,我的问题如下。我有一个路径列表,我想获得多个公共前缀。例如,假设我有:

['/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']

我想要有:

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

因为前两个有一个共同的前缀,即目录。希望我说清楚了,

rubik

编辑:我的路径是 Python Egg 和一些可执行文件。我想删除整个 Egg,而不是其中的目录,例如 EGG-INFOpkg_name。所以它必须是 /usr/.../dist-packages/pkg_name-0.1-py2.7.egg/。另一个路径,因为它是可执行文件,所以保持原样。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‘画卷フ 2024-12-06 11:11:29

问题没有明确定义。在这种情况下,您想要什么:

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

是一个 /usr 还是两个:/usr/bin /usr 还是三个?

无论哪种情况,算法都将如下所示:

  1. 对列表进行排序
  2. ,获取第一个元素,并对第 2 个、第 3 个、...、第 i 个元素执行 os.path.commonprefix() ,直到公共前缀为不是 /;这将是您的第一组
  3. ,从第 (i+1) 次开始重复步骤 2

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文