返回序列中的重复项

发布于 2024-12-14 11:50:48 字数 190 浏览 2 评论 0原文

我能想到的最好的办法是:

(defn dups [seq]
  (map (fn [[id freq]] id) 
       (filter (fn [[id freq]] (> freq 1))
               (frequencies seq))))

有更简洁的方法吗?

The best I could come up with was:

(defn dups [seq]
  (map (fn [[id freq]] id) 
       (filter (fn [[id freq]] (> freq 1))
               (frequencies seq))))

Is there a more concise way?

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

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

发布评论

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

评论(5

哆啦不做梦 2024-12-21 11:50:48

使用列表理解:

(defn dups [seq]
  (for [[id freq] (frequencies seq)  ;; get the frequencies, destructure
        :when (> freq 1)]            ;; this is the filter condition
   id))                              ;; just need the id, not the frequency

Use a list comprehension:

(defn dups [seq]
  (for [[id freq] (frequencies seq)  ;; get the frequencies, destructure
        :when (> freq 1)]            ;; this is the filter condition
   id))                              ;; just need the id, not the frequency
猫卆 2024-12-21 11:50:48
(map key (remove (comp #{1} val) 
                 (frequencies seq)))
(map key (remove (comp #{1} val) 
                 (frequencies seq)))
流绪微梦 2024-12-21 11:50:48

如果您想根据列表中项目的某些属性查找重复项(即,它是映射列表或记录/java 对象列表)

(defn dups-with-function
  [seq f]
  (->> seq
       (group-by f)
       ; filter out map entries where its value has only 1 item 
       (remove #(= 1 (count (val %))))))

(let [seq [{:attribute    :one
            :other-things :bob}
           {:attribute    :one
            :other-things :smith}
           {:attribute    :two
            :other-things :blah}]]
  (dups-with-function seq :attribute))

输出:

 ([:one
   [{:attribute :one, :other-things :bob}
    {:attribute :one, :other-things :smith}]])

如果您有一个 java 对象列表并且想要查找所有名字重复的人:

(dups-with-function my-list #(.getFirstName %))

If you'd like to find duplicates based off some property of the items in the list (i.e. it's a list of maps or a list of records/java objects)

(defn dups-with-function
  [seq f]
  (->> seq
       (group-by f)
       ; filter out map entries where its value has only 1 item 
       (remove #(= 1 (count (val %))))))

(let [seq [{:attribute    :one
            :other-things :bob}
           {:attribute    :one
            :other-things :smith}
           {:attribute    :two
            :other-things :blah}]]
  (dups-with-function seq :attribute))

outputs:

 ([:one
   [{:attribute :one, :other-things :bob}
    {:attribute :one, :other-things :smith}]])

If you have a list of java objects and you want to find all the ones with duplicate first names:

(dups-with-function my-list #(.getFirstName %))
痕至 2024-12-21 11:50:48

最小过滤器和频率 oneliner 可以完成这项工作:

(filter #(< 1 ((frequencies col) %)) col)

但是它在大数据上表现不佳。您必须通过说以下内容来帮助编译器:

(let [ freqs (frequencies col) ]
  (filter #(< 1 (freqs %)) col))

Minimal filter and frequencies oneliner that does the job:

(filter #(< 1 ((frequencies col) %)) col)

However it performs poorly on large data. You will have to help the compiler by saying:

(let [ freqs (frequencies col) ]
  (filter #(< 1 (freqs %)) col))
草莓酥 2024-12-21 11:50:48

some 是完美的函数。

(defn dups [coll]
  (some (fn [[k v]] (when (< 1 v) k))
    (frequencies coll)))

但是,它基本上与列表理解相同。

some is the perfect function for this.

(defn dups [coll]
  (some (fn [[k v]] (when (< 1 v) k))
    (frequencies coll)))

However, it basically does the same as the list comprehension.

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