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've got two lists that look like this

list1={{"1/01/2010 6:15", 0.0565625}, {"11/06/2010 0:15",0}, {"11/06/2010 0:30", 0},{"11/06/2010 0:45",0}, {"11/06/2010 1:00", 0}}

list2={{"01/01/2010 06:15", 0.04375}, {"01/01/2010 06:30",0.04375}, {"01/01/2010 06:45", 0.04375}, {"01/01/2010 07:00",0.04375}, {"01/01/2010 07:15", 0.04375}}

What I want to do is create a list that looks like this;

list3={{"1/01/2010 6:15", 0.0565625, 0.04375},{{"01/01/2010 06:30",,0.04375},......}

Either list may have gaps in it. It's not important that all the gaps be filled (if the date is missing from both list1&2 then it can be missing from list3). What is important is that it's fast, the data set is about 100,000 records.

share|improve this question
    
Will the date strings be listed identically? - i see you have differences: "1/01/2010 6:15" vs "01/01/2010 06:15" –  Vitaliy Kaurov Nov 29 '12 at 22:54
    
The date strings will be identical. I can convert them to DateLists but I though that would not be necessary because I don't need to fill the gaps. –  Cam Nov 29 '12 at 23:04
    
Sorry Vitaliy, when I read you question more carefully I understood. You're correct, they are not identical strings (I didn't notice that). Maybe I will need to parse them to a DateList after all. –  Cam Nov 29 '12 at 23:19
    
Yes I figured that ;) See my answer. –  Vitaliy Kaurov Nov 29 '12 at 23:21
add comment

2 Answers 2

up vote 9 down vote accepted

I confess to being allergic to database operations that require data to be in a particular sort order: it's too easy for huge mistakes to creep in. What is needed here is to turn the source list (say, list1) into a lookup table so it reliably returns its value (second element in the list) when given its key (first element in the list).

To assure reliability of key matches, let's first convert strings into bona fide dates:

l1 = {DateList[First[#]], Last[#]} & /@  list1;
l2 = {DateList[First[#]], Last[#]} & /@  list2;

Now convert the source list into a rule dispatch table:

rules = Dispatch[Flatten[({First[#] -> Sequence @@ #} & /@  l1), 1]]

(This can take some time with 100,000 entries: my system requires almost ten seconds. I consider the wait to be worthwhile. But be careful if the source list is much longer than this!)

These rules can repeatedly be applied to any number of target lists:

l2 /. rules // MatrixForm

$\left( \begin{array}{c} \{\{2010,1,1,6,15,0.\},0.0565625,0.04375\} \\ \{\{2010,1,1,6,30,0.\},0.04375\} \\ \{\{2010,1,1,6,45,0.\},0.04375\} \\ \{\{2010,1,1,7,0,0.\},0.04375\} \\ \{\{2010,1,1,7,15,0.\},0.04375\} \end{array} \right)$

When the target has $100000$ elements and there are $100000$ source elements, this last step still takes only $0.25$ seconds.

share|improve this answer
    
That seems to work. It's the l1 = {DateList[First[#]], Last[#]} & /@ list1; operations that take the time though. I got around 50 seconds on my PC. I had a play around with AbsoluteTime in the hope to speed it up but it didn't make much difference. –  Cam Nov 30 '12 at 0:15
1  
In some ways it's good that converting strings to dates is the rate-determining step: that means we shouldn't be too concerned about optimizing the join itself, and that gives us the flexibility to craft code that executes correctly in most circumstances. It still looks like a good idea to do the string-to-date conversion, despite the time it takes, because that avoids subtle problems created by potential failures to match two different strings that otherwise represent the same date. Alternatively: do your database operations with a database management system! –  whuber Nov 30 '12 at 13:06
1  
I up voted this sometime ago, but I have found it so useful for a wide range of problems (beyond date lookups), I wanted to say thanks. –  Jagra Jun 30 '13 at 14:59
add comment

Your dates are not identical strings. First convert them to numeric dates:

list1d = {DateList[#[[1]]], #[[2]]} & /@ list1;
list2d = {DateList[#[[1]]], #[[2]]} & /@ list2;

Then just

    dat= Reverse[Union[Flatten[#, 1]]]&/@GatherBy[Flatten[{list1d, list2d}, 1], First];
    Column@dat

enter image description here

If you need to even out rows - as requested in comments - use PadRight:

PadRight[#, 3] & /@ dat // Grid

enter image description here

share|improve this answer
    
I've parsed the dates using DateList[{#, {"Day", "Month", "Year", "Hour", "Minute"}}] & /@ list1[[All, 1]]. It seems to work but I get manged dates. The output for me looks like this {2010, 15, 6, 1, 0.0565625, 0.04375, 0.}, {2010, 30, 6, 1, 0.04375,0.}, .... –  Cam Nov 29 '12 at 23:28
    
@Cam I completely updated the answer. –  Vitaliy Kaurov Nov 29 '12 at 23:45
    
Is there any way to make the length of the rows consistent? Like {{2010,11,6,0,15,0.},0,} or even put a zero in there if it's easier. –  Cam Nov 30 '12 at 0:07
    
@Cam You need good visual? I am not sure I understand - what do you mean "consistent"? –  Vitaliy Kaurov Nov 30 '12 at 0:15
    
What I mean is that every row will have 3 elements. It's the difference between {{2010,11,6,0,15,0.},0,} and {{2010,11,6,0,15,0.},0} –  Cam Nov 30 '12 at 0:27
show 1 more comment

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.