sorting a list based on the order of its items in another list

82 vues
Accéder directement au premier message non lu

Deyan Yotsov

non lue,
12 déc. 2017, 08:25:1712/12/2017
à clo...@googlegroups.com
Hello,

I have one list of potentially up to 2 million strings: big-list.

Then I have another list, small-list, the elements of which are a subset
of the elements of big-list.

I want to sort the elements in small-list so that they become sorted in
the same way in which they are sorted in big-list.

The most obvious way to do this, for me, is:

(filter #(some #{%} small-list) big-list)

(one by one we take the elements from big-list, we check if they are in
small-list, and if they are, we add them (in big-list order) to the result)

But of course this performs extremely poorly, especially if small-list
is say 1M elements, and big-list is 2M elements.

Suggestions?

Thank you,

Deyan

Deyan Yotsov

non lue,
12 déc. 2017, 08:29:4112/12/2017
à clo...@googlegroups.com
PS: I just realized that the code, as I wrote it, will not work, as
nesting of # is forbidden.

My actual code is:

(defn is-element-in-list [e l]
  (some #{e} l))

(filter #(is-element-in-list % small-list) big-list)

Sorry about that.

Deyan

Gary Trakhman

non lue,
12 déc. 2017, 08:31:5012/12/2017
à clo...@googlegroups.com
How about (filter (set small-list) big-list) ?

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ray Miller

non lue,
12 déc. 2017, 08:32:2012/12/2017
à clo...@googlegroups.com
Very similar to your solution: (filter (set small-list) big-list)

Deyan Yotsov

non lue,
12 déc. 2017, 10:40:4312/12/2017
à clo...@googlegroups.com

Thank you very much, Ray Miller and Gary Trakhman!


For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message