匹配两个文本文件中的字符串,并在Clojure中进行过滤

发布于 2025-02-04 19:02:47 字数 648 浏览 1 评论 0原文

我的问题几乎可以在下面的链接上回答。但是,我对以下问题有略有补充(请检查“类似问题”)。

类似

问题

的 删除了文件夹,仅应在输出中列出此文件夹(此文件夹的内容不应另外列出)。例如:

  • /fotos/nope-2018/globe

  • /fotos/fotos/nope-2018/globe/dc-40.jpg

  • /fotos/nope-2018/globe/globe/dc-41.jpg

  • 之前/fotos/nope-2018/globe/globe/dc-42.jpg

  • 之前/fotos/nope-2018/globe/globe/dc-43.jpg

如果删除了以/globe(包括/globe)开头的所有文件,则输出不应包含所有项目,而应仅包含“ globe”。如何实现这一目标,以实现给定链接中先前问的问题?

任何帮助都将受到赞赏。

My question is almost answered at the below link. However, I have a slight addition to the below question (Please check the "Similar Question").

Similar Question

My question is that:

If an entire folder was deleted, only this folder should be listed in the output (the content of this folder should not be listed additionally). As an example:

  • before/Fotos/Nope-2018/globe

  • before/Fotos/Nope-2018/globe/dc-40.jpg

  • before/Fotos/Nope-2018/globe/dc-41.jpg

  • before/Fotos/Nope-2018/globe/dc-42.jpg

  • before/Fotos/Nope-2018/globe/dc-43.jpg

If all the files starting with /globe (including /globe) is deleted, the output should not contain all the items but only "globe". How to achieve this as an addition to achieving the previously asked question in the given the link?

Any help is appreciated.

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

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

发布评论

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

评论(1

风启觞 2025-02-11 19:02:47

我正在做一些假设:

  1. 当您的“类似问题”的输出包括且仅当删除其所有文件/子文件夹时,包括文件夹(又称目录)名称。
  2. 文件路径由斜线界定。

您可以将“类似问题”的输出输出并后处理,以使所得集合中的任何元素都不是其他任何元素的前缀substring。

以下显示了实现这一目标的一种方法。

user> (def deleted-files-raw
        ["Fotos/Nope-2018/globe"
         "Fotos/Nope-2018/glob"
         "Fotos/Nope-2018/globe/asia"
         "Fotos/Nope-2018/globe/asia/dc-40.jpg"
         "Fotos/Nope-2018/globe/dc-40.jpg"
         "Fotos/Nope-2018/world/dc-40.jpg"
         "Fotos/Nope-2018/globe/dc-41.jpg"])
#'user/deleted-files-raw
user> (defn remove-files-deleted-dirs
        "`coll` is a collection of /-separated pathnames.
  Returns a collection of pathnames such that none of the pathnames
  are prefix substrings of any other pathname,
  assuming a / as the separator."
        [coll]
        (reduce (fn [acc x]
                  (if (clojure.string/starts-with? x (str (last acc) "/"))
                    acc
                    (conj acc x)))
                []
                (sort coll)))
#'user/remove-files-deleted-dirs
user> (remove-files-deleted-dirs deleted-files-raw)
["Fotos/Nope-2018/glob"
 "Fotos/Nope-2018/globe"
 "Fotos/Nope-2018/world/dc-40.jpg"]
user> 

I'm making a few assumptions:

  1. The output of your "Similar Question" includes the folder (aka directory) name if and only if all its files/sub-folders have been deleted.
  2. The file paths are delimited by a slash.

You can take the output of the "Similar Question" and post-process it such that no element of the resultant collection is a prefix substring of any other element.

The following shows one way of accomplishing that.

user> (def deleted-files-raw
        ["Fotos/Nope-2018/globe"
         "Fotos/Nope-2018/glob"
         "Fotos/Nope-2018/globe/asia"
         "Fotos/Nope-2018/globe/asia/dc-40.jpg"
         "Fotos/Nope-2018/globe/dc-40.jpg"
         "Fotos/Nope-2018/world/dc-40.jpg"
         "Fotos/Nope-2018/globe/dc-41.jpg"])
#'user/deleted-files-raw
user> (defn remove-files-deleted-dirs
        "`coll` is a collection of /-separated pathnames.
  Returns a collection of pathnames such that none of the pathnames
  are prefix substrings of any other pathname,
  assuming a / as the separator."
        [coll]
        (reduce (fn [acc x]
                  (if (clojure.string/starts-with? x (str (last acc) "/"))
                    acc
                    (conj acc x)))
                []
                (sort coll)))
#'user/remove-files-deleted-dirs
user> (remove-files-deleted-dirs deleted-files-raw)
["Fotos/Nope-2018/glob"
 "Fotos/Nope-2018/globe"
 "Fotos/Nope-2018/world/dc-40.jpg"]
user> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文