Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am running this on a very large list, it is important to notice that some times the dates in v occure several times, and I want the selected list to show the same amount. This produces the right output but it is way to slow. How can I speed it up.

both={{{1996, 1, 5, 0, 0, 0.},2}, {{1996, 1, 8, 0, 0, 0.},3}, {{1996, 1, 11, 0, 0, \
0.},1}}
v={{1996, 1, 5, 0, 0, 0.}, {1996, 1, 8, 0, 0, 0.}, {1996, 1, 11, 0, 0, 
  0.}, {1996, 1, 11, 0, 0, 0.}, {1996, 1, 11, 0, 0, 0.}, {1996, 1, 11,
   0, 0, 0.}, {1996, 1, 11, 0, 0, 0.}, {1996, 1, 11, 0, 0, 0.}, {1996,
   1, 11, 0, 0, 0.}, {1996, 1, 12, 0, 0, 0.}, {1996, 1, 15, 0, 0, 
  0.}, {1996, 1, 16, 0, 0, 0.}}

Select[both, Function[{f}, MatchQ[f[[1]], #]], 1] & /@ v

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Something like

v /. Dispatch[Thread[both[[All, 1]] -> both]]

should operate much faster, especially on large lists.

Surround with something like cases, e.g.,

Cases[v /. Dispatch[Thread[both[[All, 1]] -> both]], {{__}, _}]

For only "changed" to be in list.

Many, many ways to do this, btw... e.g., if the output desired is always in order of both as in your example, and you only want "matched" elements modified, this should be very quick:

MapThread[ConstantArray, {both,Tally[Join[both[[All, 1]], v]][[;; Length@both, 2]] - 1}]

Note that these output slightly different nesting than your small example, if that's an issue, let me know. You can use Flatten or Join to rearrange result less non-matches, e.g.:

Join @@ MapThread[ConstantArray, {both, 
   Tally[Join[both[[All, 1]], v]][[;; Length@both, 2]] - 1}] 
share|improve this answer
    
I see that I have to check if function goes true on the entire list and therefor it goes slow, however in this case you end up with many values that I need to delete in this answere its "deleted" by only picking the values that match the pattern I want, however there has to be a simpler and quicker way? I would like to point out that my dataset is close to 100 000 000 inputs. –  ALEXANDER Mar 15 at 23:00
    
Perfect thank you! –  ALEXANDER Mar 15 at 23:05
    
@ALEXANDER: LOL, guess I was mind-reading... glad it helped, and thanks for accept. –  rasher Mar 15 at 23:07
    
Haha yes you were! Add it to your CV! –  ALEXANDER Mar 15 at 23:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.