10
我需要建立一个部分Inverted Index
。例如:时间效率部分倒立索引建设
l = {{x, {h, a, b, c}}, {y, {c, d, e}}}
iI[l]
(*
-> {{a, {x}}, {b, {x}}, {c, {x, y}}, {d, {y}}, {e, {y}}, {h, {x}}}
*)
我认为它很清楚它的功能。在输入列表中,{x,y ...}是唯一的,而{a,b,c,..}不是。输出应该按照#[[1]]
的顺序排列。
现在,我这样做:
iI[list_List] := {#, list[[Position[list, #][[All, 1]]]][[All, 1]]} & /@
([email protected]@[email protected]@list)
但它看起来这样一件容易的事太令人费解,似乎太慢了,我应该能够应付军团。
试驾对比结果:
words = DictionaryLookup[];
abWords = DictionaryLookup["ab" ~~ ___];
l = {#, RandomChoice[abWords, RandomInteger[{1, 30}]]} & /@ words[[1 ;; 3000]];
[email protected]@iI[l]
(*
-> 5.312
*)
因此,任何想法的加速?
荣耀的一步http://i.stack.imgur.com/EqlqO.png :) –
确实不错。 '线程'的名单甚至没有必要;你可以做一些像iI [list_]:= Sort [Reap [Sow @@@ list,_,List] [[2]]]'使它更快。 – Heike
@真的,谢谢。当我开发代码时,我首先想到它应该是'Sow [#2,#1]&',如果这是真的,则需要'Thread'。当我意识到订购是直接的,我忘了将其删除。将编辑使用您的版本。 –